基于以下.ps1程序。编写一个程序来检查用户" shu"如果不存在则会创建它。
Write-Host "checking users"$testUser="hgallo"
$checkUser = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select-String -Pattern $testUser
if($checkUser -ne $null) {
Write-Host "user $testUser found"
} else {
Write-Host "user $testUser not found"
}
这是我的功课,我不擅长编码 你能帮帮我吗。
以下是错误Select-String
:
Cannot bind argument to parameter 'Pattern' because it is null.
At line:1 char:107
+ $checkUser=Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | select-string -pattern <<<< $tes tUser
+ CategoryInfo : InvalidData: (:) [Select-St
答案 0 :(得分:2)
VonPryz是对的,
请尝试以下代码
Write-Host "checking users"
$testUser="hgallo"
$checkUser = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select-String -Pattern $testUser
if($checkUser -ne $null) {
Write-Host "user $testUser found"
} else {
Write-Host "user $testUser not found"
}
代码中的错误表明-Pattern
参数失败,因为变量$testuser
为空。这是因为第一行代码的格式化。
格式错误的代码:
Write-Host "checking users"$testUser="hgallo"
正确格式化代码:
Write-Host "checking users"
$testUser="hgallo"