Showing posts with label Get-WmiObject. Show all posts
Showing posts with label Get-WmiObject. Show all posts

Friday, October 31, 2008

Defrag Your Servers Remotely with PowerShell

Another quick and dirty script.  I've got a couple servers that are running applications that suffer from disk thrashing, and are running Windows Server 2003.  A defrag tends to noticeably increase performance.  With this script I can kick off a defrag from my desk whenever I want.






Friday, August 29, 2008

Get All Shared Printers with WMI and PowerShell

I whipped up this handy dandy script yesterday for work, and I think it makes a good simple example script, so I'm posting it here:

# Finds all servers that are hosting printers.
#
# Author: Tojo2000 
#
# Note: Requires a text file with one server per line named 'winserv.txt'.

$credential = Get-Credential
$servers = gc winserv.txt | sort

echo "ServerName`tShareName`tName`tDriverName`tStatus" > .\printers.txt

foreach ($server in $servers) {
  echo $server
  $ping = gwmi Win32_PingStatus -Filter "address = `"$server`""

  if ((!$ping.StatusCode) -and (!$ping.PrimaryAddressResolutionStatus)) {
    $printers = gwmi -Credential $credential -ComputerName $server -class Win32_Printer -Filter 'Shared = "True"'

    foreach ($printer in $printers) {
      if ($printer) {
        $output = $server, $printer.ShareName, $printer.Name, $printer.DriverName, $printer.Status
        echo ($output | Join-String2 "`t") >> .\printers.txt
      }
    }
  }
}

__________________________________________

# Join-String2
# Args:
#   $join_string: the string to paste between each element of the string array
#
# Returns:
#   The resulting string to the pipeline

function global:Join-String2 ([string]$join_string) {
  $output = '';
  $input_array = @($input);
  
  foreach ($string in $input_array[0..($input_array.Length - 2)]) {
    $output += $string;
  $output += $join_string;
  }
  $output += $input_array[-1];
  
  Write-Output $output;
}


There are a couple of things worthy of note here.  The Get-Credential cmdlet pops up a prompt to enter a username and password and stores it as a SecureString This is vastly more secure than storing the string in your script or typing it in to the console window.  Get-WMIObject, like many cmdlets, has a -Credential option that takes a credential as an input and runs the cmdlet under that account.  You can also specify a username here, and Get-Credential will be invoked to prompt you for the password.

The Join-String2 function is because there is already a cmdlet that is part of the PowerShell Comunity Extensions called Join-String, but apparently it freaks out if any of the values being printed are null, so I created a function that does a basic join.

The -Filter option of Get-WMIObject is basically just the "where clause" of the wmi query, which is to say that

gwmi Win32_Printer -Filter 'Shared = "True"'

is the same thing as

gwmi -Query 'Select * From Win32_Printer Where Shared = "True"'

It's just a lot more PowerShell-ish and a little less verbose.

Friday, August 15, 2008

Getting Network Adapter Information Using Get-WMIObject and GetRelated()

WMI stores network adapter information in two classes: Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration.  The former has basic information about the network adapter, and the latter has the IP configuration, etc.

The last time I had to do this, had to look up both objects and compare them to see that the property that linked the two classes was called Index and then write this to output the Win32_NetworkAdapter object, the associated Win32_NetworkAdapterConfiguration object, and then a line of dashes to separate the adapters.

gwmi Win32_NetworkAdapterConfiguration| %{gwmi -query "Select * from Win32_NetworkAdapter Where Index = $($_.Index)"; $_;  '------------------'}

As I discoverd from another post by /\/\o\/\/ (aka The PowerShell Guy), it turns out that the objects returned by Get-WmiObject have a nifty method called GetRelated that will do that work for you, so you can do this instead:

gwmi Win32_NetworkAdapterConfiguration | %{$_.GetRelated('Win32_NetworkAdapter'); $_;  '------------------'}