Jay Thomas
asked on
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.tx t
foreach($computer in $computers)
{
invoke-command -computername $computer -scriptblock{$version=(Get -Command powershell).FileVersionInf o.FileVers ion }
$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
$computers=get-content c:\work\temp\serverlist.tx
foreach($computer in $computers)
{
invoke-command -computername $computer -scriptblock{$version=(Get
$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
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.tx t and what gets stored in $Computer
So as Alan suggested, check your input from c:\work\temp\serverlist.tx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
@ 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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.)
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).
Hi
What did you get from the script I posted above?
Alan.
What did you get from the script I posted above?
Alan.
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.