Author Posts

February 10, 2016 at 5:55 am

Hi,
I run script on remote computers and this is what I get:

ProductVersion FileVersion FileName PSComputerName
————– ———– ——– ————–
1.16.03 1.16.03 C:\Program Files\DGT\Stanowisko Wsparcia\Awizo.exe localhost
1.16.03 1.16.03 C:\Program Files\DGT\Stanowisko Wsparcia\Awizo.exe localhost
1.16.03 1.16.03 C:\Program Files\DGT\Stanowisko Wsparcia\Awizo.exe localhost
1.16.03 1.16.03 C:\Program Files\DGT\Stanowisko Wsparcia\Awizo.exe localhost
1.16.03 1.16.03 C:\Program Files\DGT\Stanowisko Wsparcia\Awizo.exe localhost

The script is working OK, I get the actual data from computers but why don't I get the proper ComputerName ?

Here is my script:

workflow foreachptest
{
	[string[]]$servers = Get-Content "C:\computers.txt"
	$pass = cat C:\Pass.txt |ConvertTo-SecureString
	$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Administrator",$pass
	
	ForEach -Parallel($server in $servers)
	{
		InlineScript
		{
		   Invoke-Command -ComputerName $USING:server -Credential $USING:mycred -ScriptBlock {Get-Process Awizo -ErrorAction silentlycontinue -fileversioninfo}						 
		}	
	}
}
foreachptest

February 10, 2016 at 7:04 am

What's in computers.txt file?

February 10, 2016 at 11:13 am

txt file contains computers ip, like this:

192.168.1.1
192.168.1.2
192.168.1.3

It works ok in normal script, problem accur when I use Workflow.

February 10, 2016 at 11:42 am

Because you are invoking the command remotely, the gwmi cmdlet is technically being run on the localhost, and is thus reporting so.

February 10, 2016 at 12:22 pm

Bob, your thinking makes sense but...

When I do the same thing but not in workflow I get output with computers ip.

Script

	[string[]]$servers = Get-Content "C:\computers.txt"
	$pass = cat C:\Pass.txt |ConvertTo-SecureString
	$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Admininistrator",$pass
	
	ForEach ($server in $servers)
	{
		Invoke-Command -ComputerName $server -Credential $mycred {Get-Process Awizo -ErrorAction silentlycontinue -fileversioninfo}				 
	}

^^Above script give me:

ProductVersion FileVersion FileName PSComputerName
————– ———– ——– ————–
1.16.03 1.16.03 C:\Program Files\DG... 192.168.1.1
1.16.03 1.16.03 C:\Program Files\DG... 192.168.1.2

February 10, 2016 at 1:12 pm

[Net.DNS]::GetHostEntry($Server)
will take the ip address and get the hostname

February 10, 2016 at 1:51 pm

Przemek,

Code within a PowerShell workflow is not run by PowerShell. When you define a workflow, PowerShell converts the code to XAML to be run in Windows Workflow.

Do not expect anything to behave exactly the same in a Workflow as it does in PowerShell. Most things do, most of the time, but not everything, and not all of the time. Test everything.