Invoke-Command – no console output?

When I run this script, it seems to work, but I get no output at all to the console. How can I get output – either a logfile or on the console? I know I must be missing something, but nothing I’ve tried seems to give me output.

#v0.3 - Restructure loop to place the user look INSIDE the Invoke-Command script block, allowing only one connection to each computer. # This should significantly decrease runtime. # # Add a Test-Connection to reduce wait time for unavailable servers. # NOTE: This requires ICMP packet travel. # # Note for later: Look into running the commands in parallel, as well as adding logging somewhere besides the display. #Install-Module -Name LocalAccount Import-Module Microsoft.Powershell.LocalAccounts $Users = Import-CSV userlist.csv | Select UserName, Password, RealName $Computers = Get-Content shortcomputerlist.txt $ExpirationDays = Read-Host "Please enter the number of days from today these accounts should expire" $ExpirationDate = (Get-Date).AddDays($ExpirationDays) ForEach ($Computer in $Computers) { if (Test-Connection -ComputerName $Computer -Count 1 -ErrorAction SilentlyContinue) { $ScriptBlock = { param($User,$ExpirationDate,$Computer) $VerbosePreference='Continue' $LocalAccounts = Get-LocalUser $LocalAdmin = Get-LocalGroupMember -Group "Administrators" | Select Name ForEach ($User in $Users) { $Name = $User.Username $Fullname = $User.RealName $Password = $User.Password $Description = "Local account for " + $FullName + " in compliance with S2T2" If ($LocalAccounts.Name -match $Name) { Write-Host "Account $Name already exists on $computer" If ($Name -match $LocalAdmin) { Write-Host "Account $Name is a local administrator on $computer" } Else { Add-LocalGroupMember -Group "Administrators" -Member $Name -Verbose -WhatIf Write-Host "Account $Name has been added to the local Administrators group on $Computer" } } Else { $SecurePassword = ConvertTo-SecureString $Password –AsPlainText –Force New-LocalUser -Name $Name -AccountExpires $ExpirationDate -FullName $FullName -Password $SecurePassword -UserMayNotChangePassword -PasswordNeverExpires -Verbose -WhatIf Write-Host "Account" $Name "has been created on $Computer" Add-LocalGroupMember -Group "Administrators" -Member $Name -Verbose -WhatIf Write-Host "Account $Name has been added to the local Administrators group on $Computer" } } } Invoke-Command -ArgumentList ($User,$ExpirationDate,$Computer) -ScriptBlock $ScriptBlock 4>&1 -ComputerName $Computers } Else { Write-Host "Unable to ping $Computer" } } 

submitted by /u/Team503
[link] [comments]

Leave a Reply