I can't unerstand!

Can some ine help me ?
why this work.:
$nomepc = read-host -prompt ‘inserisci nome PC’
Get-WmiObject –ComputerName $nomepc –Class Win32_ComputerSystem | Select-Object UserName

and this not:
$name = import-csv “E:\appoggio\Scripts\Export attributo\Export email\lista.txt”
foreach($nomepc in $name)
{
Get-WmiObject –ComputerName $nomepc –Class Win32_ComputerSystem | Select-Object UserName
}

the value on $nomepc is correct.

this is the output :
Get-WmiObject : The RPC server is unavailable.
At line:5 char:1

  • Get-WmiObject –ComputerName $nomepc –Class Win32_ComputerSystem | Sel …

@Robertofalchi Welcome PowerShell.Org forums.

Most likely there is a space in the computer name in the text file.
You can use Trim() as a safety mechanism.
Get-WmiObject –ComputerName $nomepc.Trim() –Class Win32_ComputerSystem

PS: When you post code, please use the code posting option available in the editor (on top when posting).

Thankyou Kyprasoon,
I try it but now the error messag is:
Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named ‘trim’

$name = import-csv “E:\appoggio\Scripts\Export attributo\Export email\lista.txt”

foreach( $nomepc in $name ){
    Get-WmiObject –ComputerName $nomepc.Trim() –Class Win32_ComputerSystem | Select-Object UserName
}

When you import CSV you need to reference the property name. Assuming your CSV header for computers is Name, then you’d need to check $nomepc.Name

1 Like

Thankyou krzydoug,
now it works

:+1:

1 Like