Win32_TCPIPPrinterPort WMI query is empty - powershell

When I query WMI (by any method so far) for printers:
select * from Win32_Printer
I get normal results. When I try to query for ports:
select * from Win32_TCPIPPrinterPort
the query "succeeds" but I get no results. There are over 100 ports on this server, but neither prnport.vbs, wbemtest, powershell, or my own code are getting any results.
Operating system in 2003R2 and the user has admin privs.

Turns out that local ports (and consequently redirected locals like ghostscript etc) are NOT listed by this WMI query. They are stored directly in the registry. This means that normal scripting to migrate these printer parameters will get the printers but not the port information.
They are stored in:
HKLM\SYSTEM\CurrentControlSet\Control\Print\Monitors\Redirected Port\Ports
Live and learn.

I have a couple print servers with many queues on them and they are running on MS Windows 2003. I required the port configuration as well. Namely queue and protocol. I nested the following commands in my foreach loop and I was able to get the data I needed:
$PrintQueue = (Get-ItemProperty -Path Registry::"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\$Queue" -Name Queue).Queue
$Protocol = (Get-ItemProperty -Path Registry::"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\$Queue" -Name Protocol).Protocol
$Queue variable in the path string above is the print queue variable in my foreach loop. Change it to the Print Queue name as needed or set the variable earlier in the code.
I also ran into print queues that did not have a Queue value set so I added a if statement to set one, which I required in order to feed this data to another script that installs the print queue on a 2012R2 server.
if($PrintQueue = $null){
$PrintQueue = 'Print'
}#if
Hope this helps.

Related

Bulk IP configuration using CSV input

I have a CSV file with the following values for each machine I want to remotely reconfigure using static IP
name,nic,ip,mask,defaultgw
I was hoping to be able to reconfigure the IPs for each listed but if I have more than one machine listed the script gets stuck. This is because at the end of the first loop iteration, unless I manually do an ipconfig /flushdns on the server the script is running from, I will lose connection to the server being configured and the script just hangs leaving the rest of the servers. What I have so far is this:
$csv = import-csv "c:\scripts\builds\machines.csv"
foreach ($Row in $csv) {
$machine = $Row.name
$Nic = $row.Nic
$address = $row.IP
$mask =$row.mask
$defaultgw = $row.gw
invoke-command -computername $machine -scriptblock { Get-NetIpAddress - InterfaceAlias $using:nic | New-NetIPAddress -ipaddress $using:address -PrefixLength $using:mask -DefaultGateway $using:defaultgw | Register-DnsClient}}
}
Can this be done using workflows or just simple start-job?
I suspect you're losing the connection to the remote machine once you change the IP address, while the local machine hangs trying to keep the connection.
Try making the call, and dropping off a payload, then running it after you disconnect.
That is, upload the code, then spawn it, then disconnect before it has a chance to run (add a sleep to the remote code maybe?). This way, you can launch your payload and disconnect before it affects you.
For example, you could copy a file to that machine with the values it needs, then schedule a task to run in 5 seconds in the future, then disconnect before it runs. The task will then run, and since you're already disconnected, you won't be affected by it.
You might also consider DHCP with static reservations. It's far easier to manage than what you're trying to do here.

Locate process running in all my servers using powershell

Is there a way to find or list if a specific process is runing in all servers in my domain using powershell?
For exemple, I would like to list all servers in my domain that is running "abc.exe"?
the CIM/WMI cmdlets can get the process list from remote systems [unless they are blocked]. i've only the one system to test with, but this will work with multiple systems listed in the ComputerName parameter.
if you have an older version of PoSh [pre-v-3, i think] you may need to use WMI instead of CIM.
$GCIMI_Params = #{
ClassName = 'CIM_process'
Filter = "Name = 'Firefox.exe'"
ComputerName = 'LocalHost'
}
Get-CimInstance #GCIMI_Params
that returned 7 firefox.exe processes - roughly the number of tabs i have open at the moment.

Looking for a Powershell Script to check if Volume Shadow Copy is enabled

Sorry for asking, new in PowerShell. Looking for a Powershell Script to check if Volume Shadow Copy is enabled.
Couldn't find any useful and functional script.
Where did you look?
There are a number samples of these all over the web.
For example, using the script downloadable from here:
Get Shadow Copy Statistics
If you use Shadow Copies of Shared Folders (Previous Versions), this
script may help you keep on eye on how much history you have, the
average snapshot size, whether you are hitting storage area limits or
the 64 shadow copies per volume limit.
https://gallery.technet.microsoft.com/scriptcenter/Get-Shadow-Copy-Statistics-79e05a57
You can use it's example to get stats of such items. Meaning, if you get any results, then of course it's enabled.
#Query the local machine
.\Get-ShadowCopyStats.ps1 -ServerName .
#Query a remote machine
.\Get-ShadowCopyStats.ps1 -ServerName FS01
#Query multiple remote machines by passing an array
.\Get-ShadowCopyStats.ps1 -ServerName FS01,FS02
#Since it's the first parameter, you don't have to include -ServerName in the command:
.\Get-ShadowCopyStats.ps1 FS01,FS02
#Query multiple remote machines by passing them to the script down the pipeline
"FS01","FS02" | .\Get-ShadowCopyStats.ps1
Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=company,DC=tld" | .\Get-ShadowCopyStats.ps1 -ShowAllVolumes | Tee-Object -Variable ShadowCopyStats
$ShadowCopyStats | Select * | Export-Csv -NoTypeInformation .\ShadowCopyStats.csv
Or this one...
Get Remote Shadow Volume Information With Powershell
Gather the remote shadow volume information for one or more systems
using wmi, alternate credentials, and multiple runspaces. Function
supports custom timeout parameters in case of wmi problems and returns
shadow volume information, shadow copies, their providers, and
settings.
https://gallery.technet.microsoft.com/scriptcenter/Get-Remote-Shadow-Volume-e5a72619
RemoteShadowCopyInformation -ComputerName 'Server2' -Credential $cred).ShadowCopyVolumes
when the shadowcopy is enabled, there will be a scheduled task created
$allTasks = Get-ScheduledTask
foreach ($task in $allTasks) {
if ($task.TaskName.Contains("ShadowCopyVolume")) {
#get volumeid & drive letter which shadowcopy is enabled
$allVolumes = Get-Volume
foreach ($volume in $allVolumes) {
if ($volume.ObjectId.Contains(($task.TaskName.Split("{")[1]).Split("}")[0])) {
write-host ($volume.driveletter + ":\ is enabled")
}
}
}
}

using powershell to check active IP's or MAC's connected to router

I want to run a Powershell script that talks to the router/AP and figures out what IP (I have Reserved IPS) or MAC address is currently connected to the Router/AP. The script would output what is connected so that I could see "who's home".
At first I used IE though powershell logging into the router and trying to capture data of the wifi client page but I don't think that is the way to go. Is there another way to do this? A way to scan the network without worrying about logging into the router?
If you have DNS resolution on the names of your PCs, you can try this out.
$Computer = "value or foreach loop of values"
$IPAddress = ([System.Net.Dns]::GetHostByName($Computer).AddressList[0]).IpAddressToString
$IPMAC = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Computer
$MACAddress = ($IPMAC | where { $_.IpAddress -eq $IPAddress}).MACAddress
I have tested this with a couple individual names and a foreach loop getting the names from a txt or csv file, and I tested using
write-host $IPAddress $MACAddress
at the end for a sanity check.
If you want to verify an up/down state of the computer before querying, try using the 'test-connection' powershell command (basically a ping that grabs the results)

Starting an exe file with parameters on a remote PC

We have a program running on about 400 PCs (All W7). This program is called Wisa.
We receive regular updates for this program, named something like wisa_update1.0.exe, wisa_update1.1.exe, wisa_update2.0.exe, etc. The users can not do the update themself due to account restrictions.
We manage to do the update once and distribute it with a copy-item to all PCs. Then with Enter-PSSession I can go to each PC and update the program with the following command:
wisa_update3.0 /verysilent
(with the argument /verysilent no questions are asked)
This is already a major gain in time, but I want to do the update more automatically.
I have a file "pc.txt" with all 400 PCs in it. I use this file already for the Copy-Item via Get-Content. Now I want to use this file to do the updates with the above command, but I can't find a good way to use a remote executable with a parameter in PowerShell.
What you want to do is load get-content -Path $PClist and then run your script actions in a foreach. You'll want to adapt this example to your own script:
$PClist = 'c:\pc.txt'
$aComputers = Get-Content -Path $PClist
foreach ($Computer in $aComputers)
{
code actions to perform
}
Also you can use multithreading and get it over with fraction of time (provided you have a good machine). The below mentioned link explains how to do it well.
http://www.get-blog.com/?p=22