randle Posted June 15, 2016 Posted June 15, 2016 Hi, I'm working on a PowerShell script to allow me to run command on remote computers. So far I have the following: $UserCredential = Get-Credential $Scriptblock = {PNPUTIL.exe -i -a [url="file://\\ServerName\itdepartment$\Drivers\Computers\Zoostorm1\System\Intel_8_Series_C220_Series_SMBus_Controller-8C22\LxPtSMB.inf"]\\ServerName\itdepartment$\Drivers\Computers\Zoostorm1\System\Intel_8_Series_C220_Series_SMBus_Controller-8C22\LxPtSMB.inf[/url]} $RemoteComputers = Get-ADComputer -Filter 'Name -like "t7s0*"' foreach ($Computer in $RemoteComputers) {Invoke-Command -ComputerName $Computer.DNSHostName -Authentication Credssp -Credential $UserCredential -ScriptBlock $Scriptblock} This is working fine but doesn't show the computer name for each object it processes against on the output of the screen so thought I'd add the following "write-host "Command completed on $Computer.DNSHostName" but currently I'm only getting it outputting on the last object. I've tried piping it out after the Invoke-Command cmdlet but that doesn't accept piping. Also the $Computer.DNSHostName is returning the DN of the object rather than just the FQDN as I would expect. Any ideas?
unixman_again Posted June 15, 2016 Posted June 15, 2016 I'm no p/s expert, but I've found this approach to be helpful to find out what is going on foreach ($Computer in $RemoteComputers) { echo $Computer | %{echo $_;} echo "============" } Basically make sure what you think is being processed, is actually being processed.
HPlum78 Posted June 15, 2016 Posted June 15, 2016 Hi Randle, I have just done the following in PS to see what I get out $adComputers = Get-ADComputer -filter {operatingSystem -like "Windows Server*"} $ScriptBlock = {ipconfig /all} ForEach ($Computer in $adComputers) { Invoke-Command -ScriptBlock $ScriptBlock write-host $Computer.DNSHostName } and this give me the following out Ethernet adapter Ethernet: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : vmxnet3 Ethernet Adapter Physical Address. . . . . . . . . : 00-50-56-BD-59-0D DHCP Enabled. . . . . . . . . . . : No Autoconfiguration Enabled . . . . : Yes Link-local IPv6 Address . . . . . : fe80::48c1:c944:db74:d3e%12(Preferred) IPv4 Address. . . . . . . . . . . : 143.210.19.210(Preferred) Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 143.210.19.1 DHCPv6 IAID . . . . . . . . . . . : 302010454 DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1E-5F-20-FE-00-50-56-BD-59-0D DNS Servers . . . . . . . . . . . : 143.210.19.11 143.210.19.12 NetBIOS over Tcpip. . . . . . . . : Enabled Tunnel adapter isatap.{8E4FDA31-5DA9-4875-A4CF-F58B4C5B5144}: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Microsoft ISATAP Adapter Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 DHCP Enabled. . . . . . . . . . . : No Autoconfiguration Enabled . . . . : Yes TIV-2K12R2WEBDV.Domain.sch.ac.uk as you can see this outs the name of the server (FQDN) So the questions are I suppose what version of PS/ WMF are you using and where are you putting the write-host/ write-output commands? If you put the write- outside of the for loop then this is why you would only get the last input in the loop (think that makes sense!) that's the only thing that spring to mind of the bat. Hope that helps H 1
randle Posted June 15, 2016 Author Posted June 15, 2016 Ahh nice one. I wasn't putting the write-host on a separate line between the braces. Looks to be working fine now although still getting the DN returned rather than the FQDN so a little more tweaking required. Thanks both for your input.
randle Posted June 15, 2016 Author Posted June 15, 2016 All sorted. Just needed quotes around my variable write-host -Foreground Yellow "Command completed on "$Computer.DNSHostName"`n"
HPlum78 Posted June 15, 2016 Posted June 15, 2016 ah yes did not think about the comment you are adding to the output.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now