2012-10-05 19 views
5

Ho letto vari metodi per convertire un array di caratteri in una stringa in PowerShell, ma nessuno di essi sembra funzionare con la mia stringa. la fonte della mia stringa è:PowerShell converte Char Array in stringa

$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 

Il WPageText risultante è un array di caratteri, quindi non posso utilizzare Select-String -Pattern "giorni" -Context

ho provato:

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

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

Le uniche cose che ho avuto successo con finora è:

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

Un modo per fare questo oltre a scrivere e leggere un file?

risposta

3

Il modo economico per fare ciò è modificare la variabile $ofs e racchiudere l'array in una stringa. $ofs è un separatore PS interno per gli array di stampa che utilizza Object.ToString() da .NET.

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

si può (dovrebbe) utilizzare anche il costruttore System.String in questo modo:

$a = "another mind blowing string" 
$result = New-Object System.String ($a,0,$a.Length) 
+0

++ per l'informazione '$ OFS'; vale la pena di raccomandare _localizing_ il cambio '$ OFS', ad esempio' & {$ OFS = ''; "$ c"} '. Nota che anche se _effective_ default è un singolo spazio, la variabile _ $ OFS' è di default _non definita_. Non sei sicuro di cosa stai raccomandando riguardo al costruttore di stringhe; '$ result = $ a' fa la stessa cosa molto più semplice ed efficiente. – mklement0

0

Qualunque sia la vostra stai cercando, penso che ti manca qualcosa che riguarda $WPageText. se si ha un aspetto è un PSCustomObject all'interno del quale si è interessati a outerText che è una stringa.

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= ... 

Così

PS C:\PowerShell> $WPageText.outerText 

Precision M6500 
Service Tag: 6WMPSN1 

Select A Different Product > 
Warranty Information 
Warranty information for this product is not available. 
7

È possibile utilizzare l'-join dell'operatore (con parti in più per dimostrare tipi di dati):

$x = "Hello World".ToCharArray(); 
$x.GetType().FullName   # returns System.Char[] 
$x.Length      # 11 as that's the length of the array 
$s = -join $x     # Join all elements of the array 
$s       # Return "Hello World" 
$s.GetType().FullName   # returns System.String 

In alternativa, il join può anche essere scritta come:

$x -join "" 

Entrambi sono legali; -join senza un LHS unisce semplicemente l'array sul suo RHS. Il secondo formato si unisce al LHS utilizzando il RHS come delimitatore. Vedi help about_Join per ulteriori informazioni.

+0

+1, ma non vedo alcuna necessità di trasmettere esplicitamente $ x a un array di caratteri. Un molto più semplice "-join $ x" funziona bene per me. –

+0

Potrebbe essere un post di sbornia dalle precedenti versioni di Powershell - onestamente non lo so; Ho appena visto questo metodo consigliato da qualche tempo. –