Alex Rivera | Logout

PowerShell convert Char Array to string

Asked 2012-10-05T00:59:02.047
13

I've read various methods for converting a char array to a string in PowerShell, but none of them seem to work with my string. The source of my string is:

$ComputerName = "6WMPSN1"
$WarrantyURL = "http://www.dell.com/support/troubleshooting/au/en/aulca1/TroubleShooting/ProductSelected/ServiceTag/$ComputerName"
$WarrantyPage = Invoke-WebRequest -Uri $WarrantyURL
$WPageText = $WarrantyPage.AllElements | Where-Object {$_.id -eq "TopContainer"} | Select-Object outerText

The resulting WPageText is an Char Array so I can't use Select-String -Pattern "days" -Context

I've tried:

$WPageText -join
[string]::Join("", ($WPageText))

as per http://softwaresalariman.blogspot.com.au/2007/12/powershell-string-and-char-sort-and.html

The only things I have been successful with so far is:

$TempFile = New-Item -ItemType File -Path $env:Temp -Name $(Get-Random)
$WPageText | Out-File -Path $TempFile
$String = Get-Content -Path $TempFile

Any way to do this aside from writing and reading a file?

Edit
Report

2 Answers

10

The cheap shot way of doing this is by modifying the $ofs variable and enclosing the array in a string. $ofs is an internal PS separator for printing arrays using Object.ToString() from .NET.

$a = "really cool string"
$c = $a.ToCharArray()
$ofs = '' # clear the separator; it is ' ' by default
"$c"

You can (should) also use the System.String constructor like this:

$a = "another mind blowing string"
$result = New-Object System.String ($a,0,$a.Length)
answered 2012-10-05T18:57:40.943
0

Whatever your are looking for, I think that you miss something concerning $WPageText. if you have a look it's a PSCustomObject inside which you are interested in outerText which is a string.

PS C:\PowerShell> $WPageText | Get-Member

   TypeName: Selected.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()                                                                                                                                            
outerText   NoteProperty System.String outerText= ...  

So

PS C:\PowerShell> $WPageText.outerText 

 Precision M6500 
Service Tag: 6WMPSN1 

 Select A Different Product > 
Warranty Information 
Warranty information for this product is not available. 
answered 2012-10-06T04:58:45.783

Your Answer