Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, February 20, 2012 9:22 PM
Hello,
Writing a script to compare hotfixes on servers, it was working if i specify $server1 and $server2, but not when I use $server2 as a multiple list of servers
$server1 = "ctx45dc20" $serverlist = get-content .\AllCTXServers.txt $Credentials = Get-Credential foreach ($server in $serverlist){ $server1HotFix = get-hotfix -computer $server1 -Credential $credentials | select HotfixId $server2HotFix = get-hotfix -computer $server -Credential $credentials | select HotfixId $comparedHotfixes = compare-object $server2HotFix $server1HotFix -IncludeEqual $result = @(); foreach ($c in $comparedHotfixes) { $kbinfo = "" | select KB,$server1,$server $kbinfo.KB = $c.InputObject.HotfixId switch ($c.SideIndicator)
This line $kbinfo = "" | select KB,$server1,$server seems to be the issue.
Not too sure how to fix this or what the problem is.
error:
Select-Object : Cannot convert System.Management.Automation.PSObject to one of the following types {System.String, System.Management.Automation.ScriptBlock}.
At C:\Documents and Settings\mik\Desktop\Scripts\hotfixes\compare hotfixes.ps1:33 char:26
+ $kbinfo = "" | select <<<< KB,$server1,$server
+ CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException
+ FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand
Thanks
All replies (21)
Monday, February 20, 2012 10:40 PM ✅Answered | 1 vote
Can you post the entire script here? It looks like you only posted a chunk of it. It almost looks like you are creating a temporary placeholder for a collection using the "" | Select method. However, you cannot supply an object here ($server, $Server1), only the property header.
This is an example of what I think you are trying to do...
$report = @()
Get-Service | ForEach {
$temp = "" | Select State, Name, DisplayName
$temp.State = $_.State
$temp.Name = $_.Name
$temp.DisplayName = $_.DisplayName
$report += $report
}
$Report
Boe Prox
Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.
Looking for a script? Check out the Script Repository
Need a script written for you? Submit a request at the Script Request Page
Monday, February 20, 2012 9:47 PM | 1 vote
When you pass "" to the pipeline for a select it does not know how to store this in the PSObject since (from what I can tell) there is no value for KB. It attempts to convert this string to a PSObject (via ScriptBlock) to assign it to the $kbinfo variable. Perhaps drop the "" | portion of that line.
Tuesday, February 21, 2012 1:48 PM
$server1 = "ctx45dcepc70"$serverlist = get-content .\AllCTXServers.txt$credentials = Get-Credential ghs_ntdomain\mikondrashovforeach ($server in $serverlist){ $server1HotFix = get-hotfix -computer $server1 -Credential $credentials | select HotfixId $server2HotFix = get-hotfix -computer $server -Credential $credentials | select HotfixId $comparedHotfixes = compare-object $server2HotFix $server1HotFix -IncludeEqual $result = @(); foreach ($c in $comparedHotfixes) { $kbinfo = "" | select KB,$server1,$server $kbinfo.KB = $c.InputObject.HotfixId switch ($c.SideIndicator) { # "==" { # write-host -Fore Green "Both servers have $($c.InputObject.HotfixId)" # $kbinfo.($server1) = $true # $kbinfo.($server) = $true # $result += $kbinfo | FT -AutoSize# } "=>" { write-host -Fore DarkBlue "$server1 has $($c.InputObject.HotfixId) but $server2 doesn't" $kbinfo.($server1) = $true $kbinfo.($server) = $false $result += $kbinfo } "<=" { write-host -Fore Magenta "$server2 has $($c.InputObject.HotfixId) but $server1 doesn't" $kbinfo.($server1) = $false $kbinfo.($server) = $true $result += $kbinfo } } } $result | FT -AutoSize >>$log }
Full code
Tuesday, February 21, 2012 3:31 PM
well apperently it wanted $kbinfo = "" | select KB,$server1,"$server", quotes on "$server" since it changes?
ok whatever but thats stupid..
Tuesday, February 21, 2012 3:34 PM
If forces the expression to evaluated to a string. As a $server object it's still a PSObject. The "" and () rules can be frustrating at times. That's for sure.
Tuesday, February 21, 2012 3:47 PM
Does doing this help?
$kbinfo = "" | select 'KB',$server1,$server
KB is acutally a Powershell operator, and I think that may be messing with you.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Tuesday, February 21, 2012 4:21 PM
Does doing this help?
$kbinfo = "" | select 'KB',$server1,$server
KB is acutally a Powershell operator, and I think that may be messing with you.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
It worked fine for me using KB, even though it is an operator:
$report = @()$temp = "" | Select KB,Test,Test1$temp.KB=1$temp.test=2$temp.test1=3$Report += $temp$report
Boe Prox
Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.
Looking for a script? Check out the Script Repository
Need a script written for you? Submit a request at the Script Request Page
Tuesday, February 21, 2012 4:25 PM
I think a numeric value has to be concatenated to the string for it to become an operator:
PS C:\Windows\System32> "kb".GetType()
IsPublic IsSerial Name BaseType
True True String System.Object
PS C:\Windows\System32> "1kb".GetType()
IsPublic IsSerial Name BaseType
True True String System.Object
PS C:\Windows\System32> 1kb.GetType()
IsPublic IsSerial Name BaseType
True True Int32 System.ValueType
PS C:\Windows\System32> kb.GetType()
An expression was expected after '('.
At line:1 char:12
+ kb.GetType( <<<< )
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedExpression
PS C:\Windows\System32> select $info = "" | select kb
Select-Object : A positional parameter cannot be found that accepts argument '='.
At line:1 char:7
+ select <<<< $info = "" | select kb
+ CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
PS C:\Windows\System32> $info = "" | select kb
PS C:\Windows\System32> $info.kb.getType()
You cannot call a method on a null-valued expression.
At line:1 char:17
+ $info.kb.getType <<<< ()
+ CategoryInfo : InvalidOperation: (getType:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
PS C:\Windows\System32> $info.kb
Tuesday, February 21, 2012 4:39 PM
I'm having a hard time getting that explanation to reconcile. $server is an element of an array that was created using get-content on a text file. It should already be a string.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Tuesday, February 21, 2012 4:54 PM
Try this and you will get the same error:
Trace-Command -Name TypeConversion -Expression {$info = "" | select kb,1,1 } -PSHost
Adding in some more tracesources will give better insight. Still playing with this one.
Tuesday, February 21, 2012 4:58 PM
I already figured out it doesn't like having numeric literals as selected property names. It will take them if they're quoted, or if it's a variable that contains the number as a string.
What I can't figure out is how it's getting to be anything other than a string via get-content.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Tuesday, February 21, 2012 5:00 PM
I suspect it has to do with the plumbing between the foreachs. I can't see anywhere else an automation object would come into the picture.
Tuesday, February 21, 2012 5:07 PM
Put a .gettype() on it inside the loop, and it says it's a string.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Tuesday, February 21, 2012 5:12 PM
Yeah, I did that.
Trace-Command -Name TypeConversion -Expression {66..90 | % { $c = kb; $c.Gettype(); foreach($alpha in $c) {$c; $_; $_.GetType(); $num.GetType(); $c.GetType()}}} -PSHost
Trying to see if there's something else buried in the mechanics that may give insight.
Tuesday, February 21, 2012 5:15 PM
I can't really reproduce what's being reported.
I'm starting to wonder if it was done in a PS session, or the ISE (or some other IDE environment).
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Tuesday, February 21, 2012 5:35 PM
I agree with mjolinor on this. I have not been able to produce an error using the code above. I only modified it enough to work on my network, but no errors have ben thrown by using it.
$server1 = "server1"$serverlist = "server2"foreach ($server in $serverlist){ $server1HotFix = get-hotfix -computer $server1 | select HotfixId $server2HotFix = get-hotfix -computer $server | select HotfixId $comparedHotfixes = compare-object $server2HotFix $server1HotFix -IncludeEqual $result = @(); foreach ($c in $comparedHotfixes) { $kbinfo = "" | select KB,$server1,$server $kbinfo.KB = $c.InputObject.HotfixId switch ($c.SideIndicator) { "=>" { write-host -Fore DarkBlue "$server1 has $($c.InputObject.HotfixId) but $server2 doesn't" $kbinfo.($server1) = $true $kbinfo.($server) = $false $result += $kbinfo } "<=" { write-host -Fore Magenta "$server2 has $($c.InputObject.HotfixId) but $server1 doesn't" $kbinfo.($server1) = $false $kbinfo.($server) = $true $result += $kbinfo } } }} $result
Everything shows up as expected. I ran this in ISE and on the console...
Boe Prox
Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.
Looking for a script? Check out the Script Repository
Need a script written for you? Submit a request at the Script Request Page
Tuesday, February 21, 2012 5:58 PM
you need more than one item in your server list or wrap it in @() otherwise
you're just passing a string in..
also, what are the odds it a weird item in the text file?
blank line? strange cr/lf?
Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Tuesday, February 21, 2012 6:01 PM
I tried some different things, and can get it to throw an error about empty strings not being allowed, but I haven't found anything that would produce the error described.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Tuesday, February 21, 2012 6:27 PM
you need more than one item in your server list or wrap it in @() otherwise
you're just passing a string in..
Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Yep, I was just putting together a quick test of just one system and got lazy :)
Boe Prox
Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.
Looking for a script? Check out the Script Repository
Need a script written for you? Submit a request at the Script Request Page
Tuesday, February 21, 2012 6:34 PM
that’s what you get for drinking all that wine! lol
Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Tuesday, February 21, 2012 6:48 PM
lol, had a feeling you were going to say that! Only had 2 glasses last night, otherwise the wife would have kicked my butt for drinking it all.
Boe Prox
Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.
Looking for a script? Check out the Script Repository
Need a script written for you? Submit a request at the Script Request Page