-6

Based on the following .ps1 program. Write a program that checks for the user "shu", if not present it will create it.

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"    
}

this is my homework and im not good at coding guys can you help me pleases.

Here is the error 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
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • The question has some serious formatting problems. It's hard to say whether it's just bad copy-paste or it is real code. Either way, this might be just a typo in the first `write-host` statement or something way different. As of now, it's all guessning. – vonPryz Jan 29 '17 at 21:12

1 Answers1

2

VonPryz is correct,

Please try the below code

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"    
}

The error in the code was stating that the -Pattern parameter was failing because the variable $testuser was null. This was because of the formatting on your first line of code.

Incorrectly Formatted Code:

Write-Host "checking users"$testUser="hgallo"

Correctly Formatted Code:

Write-Host "checking users"
$testUser="hgallo"
Lachie White
  • 1,246
  • 2
  • 14
  • 21
  • thank you VonPryz, it worked but i did not find the complete answer yet because mt teacher wants me to checks for the user "shu", if not present it will create it. if i type my user name Fahad will check if it exist will say found or not exist say not found . i need to add more tool to chech the user name – Fahad Alrashdi Jan 30 '17 at 04:39
  • 1
    Hi Fahad, this forum isnt designed to complete your homework, the code you had provided in your question had an issue. That was what i highlighted in my answer. If you are searching for a complete script to solve your problem you will not find it here – Lachie White Jan 30 '17 at 05:58
  • ok thank you im almost done with it but i have this issue please help me to fix it, i want ot add SHU user and output give me its found i added it but say wrong idk where is the problems. Write-Host "checking users" $testUser="hgallo" $checkUser = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select-String -Pattern $testUser if($checkUser -ne $null) { $testUser="shu" Write-Host "user $testUser found" } else { Write-Host "user $testUser not found" } – Fahad Alrashdi Jan 30 '17 at 06:24
  • What issue is that? – Lachie White Jan 30 '17 at 06:25
  • i want to add new user in this code and i wrote this command $testUser="shu" inside if statment to add this user calleed shu and say its found but i didnt work – Fahad Alrashdi Jan 30 '17 at 06:49
  • 1
    all you are doing there is creating a variable called '$testUser' and adding a string "shu" to it. Look into this link: http://stackoverflow.com/questions/15167069/powershell-create-local-user-account – Lachie White Jan 30 '17 at 06:54