Mark Minasi's Tech Forum
Register Calendar Latest Topics Chat
 
 
 


Reply
  Author   Comment  
jsclmedave

Administrator
Registered:
Posts: 418
Reply with quote  #1 
Updated 10/3 Edit Verbiage...


I owe Claus yet another dinner...

I was having an issue with one of my functions.  I wanted to pipe the results to a CSV or GridView like below.


Get-FireWallState | Out-GrideView

Get-FireWallState | Export-CSV "C:\Temp\thing.csv" -NoTypeInformation

This is what I was getting -

OGV.gif    



IF I added this inside of the function - which you shouldn't do as Jeff Hicks pointed out on a previous thread - it worked fine.


Claus had me perform these steps in order to explain what was happening.

Code:

$A = Get-FirewallState
$A | gm
   TypeName: System.String
Name             MemberType            Definition                                                                        
----             ----------            ----------                                                                        
Clone            Method                System.Object Clone(), System.Object ICloneable.Clone()                           
CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB), int IComparable...
Contains         Method                bool Contains(string value)                                                       
CopyTo           Method                void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) 
EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.StringCompariso...
Equals           Method                bool Equals(System.Object obj), bool Equals(string value), bool Equals(string v...
GetEnumerator    Method                System.CharEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerab...
GetHashCode      Method                int GetHashCode()                                                                 
GetType          Method                type GetType()                                                                    
GetTypeCode      Method                System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()         
IndexOf          Method                int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(c...
IndexOfAny       Method                int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int...
Insert           Method                string Insert(int startIndex, string value)                                       
IsNormalized     Method                bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizat...
LastIndexOf      Method                int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int L...
LastIndexOfAny   Method                int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startInd...
Normalize        Method                string Normalize(), string Normalize(System.Text.NormalizationForm normalizatio...
PadLeft          Method                string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)  
PadRight         Method                string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)
Remove           Method                string Remove(int startIndex, int count), string Remove(int startIndex)           
Replace          Method                string Replace(char oldChar, char newChar), string Replace(string oldValue, str...
Split            Method                string[] Split(Params char[] separator), string[] Split(char[] separator, int c...
StartsWith       Method                bool StartsWith(string value), bool StartsWith(string value, System.StringCompa...
Substring        Method                string Substring(int startIndex), string Substring(int startIndex, int length)    
ToBoolean        Method                bool IConvertible.ToBoolean(System.IFormatProvider provider)                      
ToByte           Method                byte IConvertible.ToByte(System.IFormatProvider provider)                         
ToChar           Method                char IConvertible.ToChar(System.IFormatProvider provider)                         
ToCharArray      Method                char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)              
ToDateTime       Method                datetime IConvertible.ToDateTime(System.IFormatProvider provider)                 
ToDecimal        Method                decimal IConvertible.ToDecimal(System.IFormatProvider provider)                   
ToDouble         Method                double IConvertible.ToDouble(System.IFormatProvider provider)                     
ToInt16          Method                int16 IConvertible.ToInt16(System.IFormatProvider provider)                       
ToInt32          Method                int IConvertible.ToInt32(System.IFormatProvider provider)                         
ToInt64          Method                long IConvertible.ToInt64(System.IFormatProvider provider)                        
ToLower          Method                string ToLower(), string ToLower(cultureinfo culture)                             
ToLowerInvariant Method                string ToLowerInvariant()                                                         
ToSByte          Method                sbyte IConvertible.ToSByte(System.IFormatProvider provider)                       
ToSingle         Method                float IConvertible.ToSingle(System.IFormatProvider provider)                      
ToString         Method                string ToString(), string ToString(System.IFormatProvider provider), string ICo...
ToType           Method                System.Object IConvertible.ToType(type conversionType, System.IFormatProvider p...
ToUInt16         Method                uint16 IConvertible.ToUInt16(System.IFormatProvider provider)                     
ToUInt32         Method                uint32 IConvertible.ToUInt32(System.IFormatProvider provider)                     
ToUInt64         Method                uint64 IConvertible.ToUInt64(System.IFormatProvider provider)                     
ToUpper          Method                string ToUpper(), string ToUpper(cultureinfo culture)                             
ToUpperInvariant Method                string ToUpperInvariant()                                                         
Trim             Method                string Trim(Params char[] trimChars), string Trim()                               
TrimEnd          Method                string TrimEnd(Params char[] trimChars)                                           
TrimStart        Method                string TrimStart(Params char[] trimChars)                                         
Chars            ParameterizedProperty char Chars(int index) {get;}                                                      
Length           Property              int Length {get;}                                                                 

   TypeName: System.Management.Automation.PSCustomObject
Name                   MemberType   Definition                      
----                   ----------   ----------                      
Equals                 Method       bool Equals(System.Object obj)  
GetHashCode            Method       int GetHashCode()               
GetType                Method       type GetType()                  
ToString               Method       string ToString()               
Server                 NoteProperty string Server=W7GS-CWXCVZ1      
Windows Firewall State NoteProperty string Windows Firewall State=On





TypeName: System.String  ...????


He pointed out that I had some carriage returns in the function 

Code:

    PROCESS {
        $ServerInfoReport = @()
        ForEach ($Computer in $ComputerName){
        $SessionParams.ComputerName  = $Computer
        $SessionParams.SessionOption = $Opt
        # Checking connection to Server If cannot connect drop this server and go to the next one.
        Try {
            $Check1 = Test-Connection $Computer -Count 1 -ErrorAction Stop | Select-Object Address,IPV4Address
            Write-Host 'Test-Connection successful for Server '  $Check1.Address  $Check1.IPV4Address -ForegroundColor Yellow
        } # End Try
        Catch [System.Net.NetworkInformation.PingException]{
            $ErrorMessage = $Computer + '  [CATCH]  ' + $Check1.IPV4Address + ' ' + $_.Exception.Message
            Write-Warning -Message $ErrorMessage
            $ErrorMessage | Out-File "C:\Temp\Get-FireWallState_Errors $Date.txt" -Append
            #Write-Warning -Message "[CATCH] Unable to connect to $Computer. Verify $Computer is online and try again."
            Continue
        } # End Catch
        Try {
            Write-Host -Message "Attempting to connect to $Computer using the DCOM protocol." -ForegroundColor Yellow
"`r`n"
            $CS = New-CimSession @SessionParams
        } # End Try
        Catch [Microsoft.Management.Infrastructure.CimException]{
            $ErrorMessage = $Computer + ' - ' + $Check1.IPV4Address + ' ' + $_.Exception.Message + '[CATCH] You may not have the proper access to: ' + $Computer + ' - ' + $Check1.IPV4Address
            Write-Warning -Message $ErrorMessage
"`r`n"
            $ErrorMessage | Out-File "C:\Temp\Get-FireWallState_Errors $Date.txt" -Append
            #Write-Warning "[CATCH] You may not have the proper access to $Computer 
"`r`n"
$Check1.IPV4Address"
            Continue
        } # End Catch
"`r`n"



Those were just to be able to make reading the txt on the screen easier, thats it.  

THOSE were being passed out from the function as well.  Once I removed them it started working correctly.

Code:

PS F:\> $A | gm

   TypeName: System.Management.Automation.PSCustomObject
Name                   MemberType   Definition                      
----                   ----------   ----------                      
Equals                 Method       bool Equals(System.Object obj)  
GetHashCode            Method       int GetHashCode()               
GetType                Method       type GetType()                  
ToString               Method       string ToString()               
Server                 NoteProperty string Server=W7GS-CWXCVZ1      
Windows Firewall State NoteProperty string Windows Firewall State=On




He gave me a sample to use to see it better - 
Code:

PS F:\> Function test {
1
"one"
[PSCustomObject]@{"horse" = "pig"}
999999999999999
}
test | Get-Member






I've been beating my head all week over this.  I had no idea that all of this was being passed...

THANK YOU CLAUS!


__________________
Tim Bolton @jsclmedave
Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ' '  

New to the forum? Please Read this
0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 177
Reply with quote  #2 
Yeah, and this is the first time (I know of) that Jeffrey Snover sort of semi-admitted that this design decision might have been a mistake... [wink]
__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
Mark

Hacked Mark's Facebook Account
Registered:
Posts: 261
Reply with quote  #3 
Good work, Tim.  The firewall cmdlets are, um, sort of suboptimal.  Congrats.
__________________
May I ask that everyone please populate the first name and last name in your user account profile.  Thanks!
0
jsclmedave

Administrator
Registered:
Posts: 418
Reply with quote  #4 
Quote:
Originally Posted by Mark
Good work, Tim.  The firewall cmdlets are, um, sort of suboptimal.  Congrats.


"suboptimal"  Ha! Yeah...   Another word I am hearing on a daily basis "Interesting"   Yeah,,, that is another word for THAT...   : )

Thanks Mark!

__________________
Tim Bolton @jsclmedave
Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ' '  

New to the forum? Please Read this
0
jhicks

Still Checking the Forum Out
Registered:
Posts: 2
Reply with quote  #5 
You can still add the carriage returns, just put them in the Write-Host command
Write-Host"Attempting to connect to $Computer using the DCOM protocol. `r`n" -ForegroundColor Yellow

And I hope you realized -Message is not a valid parameter. At least not on v5 now.
0
jsclmedave

Administrator
Registered:
Posts: 418
Reply with quote  #6 
Quote:
Originally Posted by jhicks
You can still add the carriage returns, just put them in the Write-Host command
Write-Host"Attempting to connect to $Computer using the DCOM protocol. `r`n" -ForegroundColor Yellow

And I hope you realized -Message is not a valid parameter. At least not on v5 now.


Thanks Jeff!!!

__________________
Tim Bolton @jsclmedave
Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ' '  

New to the forum? Please Read this
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation: