PS script to record powershell version information

Hi all, I have the start of a script which I want to run, parse a text file full of server names and record the PS version to a text file. Here is the script so far:

$computers=get-content c:\work\temp\serverlist.txt

foreach($computer in $computers)

{

invoke-command -computername $computer -scriptblock{$version=(Get-Command powershell).FileVersionInfo.FileVersion }

$result=new-object psobject

$result|Add-Member -MemberType NoteProperty -Name ComputerName -Value $computer

$result |Add-Member -MemberType NoteProperty -Name Version -Value $version

$result|out-file c:\work\temp\result.txt -append

}



But I get the error of:
Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is
not null or empty, and then try the command again.

Can anyone teak this so it works for me please?
Thank you
Jason
LVL 1
Jay ThomasAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlanConsultantCommented:
Hi Jason,

If it were me having this problem, the first thing I would do it insert a 'Write-Host' command just after you start the ForEach loop thus:

Write-Host $Computer

That will output what the script has pulled from the txt file and is trying to pass to the Invoke-Command.

If you do that, what do you get on screen?


I might also Write-Host what I got into $Computers prior to the start of the loop - again, just to see what it appears to be.


Alan.
1
Lasse BodilsenSystem AdministratorCommented:
I testet your script and it works fine.  except it don't print the version number in my test?

So as Alan suggested, check your input from c:\work\temp\serverlist.txt and what gets stored in $Computer
0
oBdACommented:
Seems like your list is either empty (maybe still open in an editor, but not saved?), or it contains empty lines.
But even with that corrected, your script won't work, because the $version variable will only be set on the remote computer, you can't use it back in the local script.
Try it like this:
Get-Content -Path C:\work\temp\serverlist.txt | Where-Object {$_} | ForEach-Object {
	$ComputerName = $_
	Write-Host "Processing '$($ComputerName)' ..."
	Try {
		Invoke-Command -ErrorAction Stop -ScriptBlock {
			$PSVersionTable | Select-Object -Property @{n='ComputerName'; e={$ENV:ComputerName}}, @{n='PSVersion'; e={$PSVersionTable['PSVersion']}}, @{n='BuildVersion'; e={$PSVersionTable['BuildVersion']}}
		}
	} Catch {
		$_.Exception.Message | Select-Object -Property @{n='ComputerName'; e={$ComputerName}}, @{n='Error'; e={$_}}
	}
} |
	Select-Object -Property ComputerName, PSVersion, BuildVersion, Error |
	Export-Csv -NoTypeInformation -Path C:\work\temp\result.txt

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Lasse BodilsenSystem AdministratorCommented:
or just set the $version before the invoke command, and then test $computer with IF, to check for empty lines.

$computers=get-content c:\work\temp\serverlist.txt

foreach($computer in $computers)
{
if($computer)
    {
    $version = invoke-command -computername $computer -scriptblock{(Get-Command powershell).FileVersionInfo.FileVersion }
    $result = new-object psobject
    $result | Add-Member -MemberType NoteProperty -Name ComputerName -Value $computer
    $result | Add-Member -MemberType NoteProperty -Name Version -Value $version
    $result | out-file c:\work\temp\result.txt -append
    }
}

Open in new window

0
Jay ThomasAuthor Commented:
Hi all, very sorry for the late response.
@ Alan. could you add your command to my script so that I can see what and where I should place it please?

@oBdA - Your script works, there was an empty line in my text file ;)

@ Lasse, what does your script do exactly please?

Thanks all.
Jason
0
AlanConsultantCommented:
Hi,

Try this to see what the list is returning:

$Computers=Get-Content C:\Work\Temp\ServerList.txt

ForEach($Computer in $Computers)

{

	Write-Host "The following computer was pulled from the $Computers array and will be used now:  '$($Computer)'"


	Invoke-Command -Computername $Computer -ScriptBlock{$Version=(Get-Command Powershell).FileVersionInfo.FileVersion}

	$Result=New-Object PSObject
	
	$Result | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer

	$Result | Add-Member -MemberType NoteProperty -Name Version -Value $Version

	$Result | Out-File C:\Work\Temp\Result.txt -Append

}

Open in new window



Post back with the results of that, then we can move on to any other issues, but you may want to just use ODBA's script above if it works already.

Thanks,

Alan.
0
Lasse BodilsenSystem AdministratorCommented:
my script does close to the same as oBdA's, but his script does have Error handling.

if($computer)
{
"Commands"
}

Will check for the string to be empty, and if it is, will not run the commands in the { } brackets, but instead skip to the next string.

Also it moves the $version variable to the start of the invoke-command, so that you set the variable on the local host, instead of the remote host. (as oBdA also mentiones.)
$version = invoke-command -computername $computer -scriptblock{(Get-Command powershell).

Open in new window

0
AlanConsultantCommented:
Hi

What did you get from the script I posted above?

Alan.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.