Process Monitoring with PowerShell

PowerShell MVP Jeff Hicks shares his script for watching processes using a WMI event subscription using the CIM cmdlets.

Source: Process Monitoring with PowerShell – Petri

$Poll = 120 #check the computer every two minutes

<#
I want to select all properties from the WMI class called CIM_InstModification.
This is a special system class that’s triggered when an object is modified.
I don’t want to find every object, as it is changed every second. So I tell WMI to check within a time frame of seconds.
When this event fires, WMI will have a new object called the TargetInstance.
This will be the changed object, and I only care about those that are Win32_Process objects.
The ISA operator accomplishes that.
The last part of the query is to limit results to those process objects, which is what the TargetInstance is, with a WorkingSetSize property of greater or equal to 500 MB.
#>

$query = “Select * from CIM_InstModification within $Poll where TargetInstance ISA ‘Win32_Process’ AND TargetInstance.WorkingSetSize>=$(1000MB)” #WMI query

<#
When you create the event subscriber, you can choose to simply record the events in your PowerShell session as matching events are detected.
Or you can take action.
In my case, I want to do a few things everytime a matching process is found.
I want to create a log file, and I want to display a popup message.
#>

$action={
#create a log file
$logPath= “C:\Work\HighMemLog.txt”
“[$(Get-Date)] Computername = $($Event.SourceEventArgs.NewEvent.SourceInstance.CSName)” | Out-File -FilePath $logPath -Append -Encoding ascii
“[$(Get-Date)] Process = $($Event.SourceEventArgs.NewEvent.SourceInstance.Name)” | Out-File -FilePath $logPath -Append -Encoding ascii
“[$(Get-Date)] Command = $($Event.SourceEventArgs.NewEvent.SourceInstance.Commandline)” | Out-File -FilePath $logPath -Append -Encoding ascii
“[$(Get-Date)] PID = $($Event.SourceEventArgs.NewEvent.SourceInstance.ProcessID)” | Out-File -FilePath $logPath -Append -Encoding ascii
“[$(Get-Date)] WS(MB) = $([math]::Round($Event.SourceEventArgs.NewEvent.SourceInstance.WorkingSetSize/1MB,2))” | Out-File -FilePath $logPath -Append -Encoding ascii
“[$(Get-Date)] $(‘*’ * 60)” | Out-File -FilePath $logPath -Append -Encoding ascii

#create a popup
$wsh = New-Object -ComObject Wscript.shell
$Title = “$(Get-Date) High Memory Alert”
$msg = @”
Process = $($Event.SourceEventArgs.NewEvent.SourceInstance.Name)
PID = $($Event.SourceEventArgs.NewEvent.SourceInstance.ProcessID)
WS(MB) = $([math]::Round($Event.SourceEventArgs.NewEvent.SourceInstance.WorkingSetSize/1MB,2))
“@

#timeout in seconds. Use -1 to require a user to click OK.
$Timeout = 10
$wsh.Popup($msg,$TimeOut,$Title,16+32)

}

#Now that I have the action scriptblock all that remains is to register the subscription with the Register-CimIndicationEvent.#
Register-CimIndicationEvent -Query $query -SourceIdentifier “HighProcessMemory” -Action $action

#You can see the registration with the Get-EventSubscriber cmdlet.
#This subscription will run for as long as my PowerShell session is running.
#The corollary is that I will need to recreate it every time I want to start monitoring.
#If this is a daily task, I could put it in my PowerShell profile script.

#If you want to get rid of the subscriber, simply unregister it.
#Get-EventSubscriber -SourceIdentifier “HighProcessMemory” | Unregister-Event

 

How to Create a Bootable USB Drive

Step 1: Using DISKPART command

  1. Insert your USB flash drive to your running computer.
  2. Run Command Prompt as administrator.
  3. Type ‘diskpart‘ on Command Prompt (without quotes) and hit Enter.
  4. Type ‘list disk‘ to view active disks on your computer and hit Enter and find the USB drive disk #.
  5. Type ‘select disk 1‘ (or whatever the USB drive is).
  6. Type ‘clean‘ and hit Enter to remove all of data in the drive.
  7. Type ‘create partition primary‘ and hit Enter. Creating a primary partition and further recognized by Windows as ‘partition 1‘.
  8. Type ‘select partition 1‘ and hit Enter. Choosing the ‘partition 1‘ for setting up it as an active partition.
  9. Type ‘active‘ and hit Enter. Activating current partition.
  10. Type ‘format fs=ntfs quick‘ and hit Enter. Formatting current partition as NTFS file system quickly.
  11. Type ‘exit‘ and hit Enter. Leaving DISKPART program but don’t close the Command Prompt instead. We would still need it for next process.

Step 2: Creating Boot Sector

Mount the ISO file to a drive.

Let us assume that the flash / USB drive is the D: drive and the DVD installer located on drive F:The first step, we will navigate Command Prompt to set installation DVD as its active directory.

  • (if you want to create boot from ISO file then, first thing to do is to mount the ISO of the Windows OS you just downloaded. Double-click the ISO file to mount it. Check the drive letter and make note of the drive letter).
  1. By default, Command Prompt’s active directory for Administrator permission is on C:\Windows\System32>. We will navigate Command Prompt to set on DVD (F:) as its active directory. Just type ‘f:‘ then hit Enter, and the active directory changed to F:
  2. Type ‘cd boot‘ and hit Enter. Active directory changed to F:\boot>.
  3. Type ‘bootsect /nt60 d:‘ and hit Enter. Creating boot sector on D: drive (USB flash drive).
  4. Type ‘exit‘ and hit Enter to close the Command Prompt. Until this step we have made a bootable USB drive successfully, and the flash drive is ready to be used as a boot media.

Step 3: Copying Installation Files

To install Windows from a bootable USB drive, we just need to copy the whole installation files contained on the DVD installer to flash drive. To do this, open the Command Prompt as in previous steps. Once it opens, type ‘xcopy f:\*.* d:\ /E /H /F‘ and then press Enter. Wait until all the files in the DVD installer copied to the flash drive. Now bootable USB drive is ready to be used for installing Windows from flash drive and you’re done !

 

Error 0x80004005 accessing CIFS from Windows server

Issue: Accessing shares hosted on third party file server (like NetApp or EMC) from Windows server 2012 returns error

Windows cannot access \\ServerName

Check the spelling of the name. Otherwise, there might be a problem with your network. To try to identify and resolve network problems, click Diagnose.

Error code: 0x80004005
Unspecified error

Cause:

With Windows server 2012 and Windows 8, Secure dialect negotiation is introduced in SMB 3.0. This feature depends upon the correct signing of error responses by all SMBv2 servers, including servers that support only protocol versions 2.0 and 2.1. Some third-party file servers do not return a signed error response. Therefore, the connection fails.

Resolution:

On the Netapp filer:

Disable SMB2 on the storage server by entering the following command: options cifs.smb2.client.enable off

options cifs.smb2.signing.required on

http://mysupport.netapp.com/NOW/cgi-bin/bol?Type=Detail&Display=474548

On the server:

Run the Powershell command:

Set-ItemProperty -Path “HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters” RequireSecureNegotiate -Value 0 –Force

or

Set-SmbClientConfiguration – RequireSecureNegotiate 0

how to solve: This product can only be installed on Windows XP SP2 and above when Installing vSphere Client 5.0 on Windows 8.

Symptoms

  • Installing vSphere Client 5.0 fails on Windows 8
  • You see the error:
    This product can only be installed on Windows XP SP2 and above

Resolution

This issue is resolved in vCenter Server 5.0 Update 2, available at VMware Downlaods.
To work around this issue:
  1. Right-click the VMware-viclient-build number.exe file and click Properties.
  2. On the Compatibility tab, select Run this program in compatibility mode for: and select Windows 7 from the list.
  3. Click OK.
  4. Run the VMware-viclient-build number.exe file.

Source: VMware KB: Installing vSphere Client 5.0 on Windows 8 fails with the error: This product can only be installed on Windows XP SP2 and above

How to Shutdown a Failover Cluster or a Node.

Shutting Down a Node

When shutting down or rebooting a node in a Failover Cluster, you first want to drain (move off) any roles running on that server (such as a virtual machine).  This ensures that the shutting down of a node is graceful to any applications running on that node.

  1. Open Failover Cluster Manager (CluAdmin.msc)
  2. Click on “Nodes”
  3. Right-click on the node name and under ‘Pause’ click on ‘Drain Roles’
  4. Under Status the node will appear as ‘Paused’.  At the bottom of the center pane click on the ‘Roles’ tab.  Once all roles have moved off this node, it is safe to shut down or reboot the node.

    To resume the node after it has been restarted…

    When the node is once again powered on and ready to be put back into service, use the Resume action to re-enable the node to host roles.

    1. Open Failover Cluster Manager (CluAdmin.msc)
    2. Click on “Nodes”
    3. Right-click on the node name and select ‘Resume’, then select either:
      1. Fail Roles Back’ – This will resume the node and move any roles which were running on the node prior to the node back.  Caution:  This could incur downtime based on the role
      2. Do Not Fail Roles Back’ – This will resume the node and not move any roles back.

Shutting Down a Node with Windows PowerShell®

  1. Open a PowerShell window as Administrator
  2. Type:  Suspend-ClusterNode -Drain
  3. Type:  Get-ClusterGroup
    1. Verify that there are no roles listed under “OwnerNode” for that node
    2. This could be scripted with the following syntax:
      PS C:\> (Get-ClusterGroup).OwnerNode –eq “NodeBeingDrained”
  4. Shutdown or restart the computer by typing either:
    1. Stop-Computer
    1. Restart-Computer

To resume the node after it has been restarted…

  1. Open a PowerShell window as Administrator
  2. Type:  Resume-ClusterNode
    1. If you wish to fail back the roles which were previously running on this node type:

PS C:\> Resume-ClusterNode –Failback Immediate

Shutting Down a Cluster

Shutting down the entire cluster involves stopping all roles and then stopping the Cluster Service on all nodes.  While you can shut down each node in the cluster individually, using the cluster UI will ensure the shutdown is done gracefully.

  1. Open Failover Cluster Manager (CluAdmin.msc)
  2. Right-click on the cluster name, select ‘More Actions’, then “Shut Down Cluster…”
  3. When prompted if you are sure you want to shut down the cluster, click “Yes”

Shutting Down a Cluster with PowerShell

  1. Open a PowerShell window as Administrator
  2. Type:  Stop-Cluster

Controlling VM Behavior on Shutdown

When the cluster is shut down, the VMs will be placed in a Saved state.  This can be controlled using the OfflineActionproperty.

To configure the shut down action for an individual VM (where “Virtual Machine” is the name of the VM):

PS C:\> Get-ClusterResource “Virtual Machine” | Set-ClusterParameter OfflineAction 1

 

 Value  Effect
 0  The VM is turned off
 1 (default)  The VM is saved
 2  The guest OS is shut down
 3  The guest OS is shut down forcibly

To start the cluster after it has been shut down

  1. Type:  Start-Cluster

Source: How to Properly Shutdown a Failover Cluster or a Node – Clustering and High-Availability – Site Home – MSDN Blogs