Skip to content

Instantly share code, notes, and snippets.

@BenNeise
BenNeise / GetUptime.ps1
Last active December 16, 2015 12:28
Gets uptime of computers and displays results in hours, mins and seconds. Written for http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/18/advanced-practice-for-2013-scripting-games.aspx
Function Get-Uptime {
<#
.Synopsis
Gets uptime of computers via WMI and displays results in hours, mins and seconds.
.Description
Gets uptime of computers and displays results in hours, mins and seconds. Written for http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/18/advanced-practice-for-2013-scripting-games.aspx
.Parameter ComputerName
The hostname, alias, or IP of the computer from which to get the uptime
@BenNeise
BenNeise / IsPingable.ps1
Created April 22, 2013 12:56
Uses WMI to ping a server, and returns TRUE if a status code of 0 is returned.
Function IsPingable {
<#
.Synopsis
Pings a server and returns TRUE or FALSE.
.Description
Uses WMI to ping a server, and returns TRUE if a status code of 0 is returned, otherwise returns FALSE. Useful for quick checks to see if a server exists and is online.
.Parameter Computer
The computer's Hostname, FQDN, or IP to be pinged.
@BenNeise
BenNeise / Get-LockedADAccounts.ps1
Last active December 16, 2015 13:29
PowerShell function to search event logs of domain controllers for event ID 4740, and returns the time the event was logged (LockedTime), the user account SamAccountName (Account), the source computer (LockedFrom), and the current locked status (Locked)
Function Get-LockedADAccounts {
<#
.Synopsis
Gets lockout events from event logs on domain controllers
.Description
Searches event logs of domain controllers for event ID 4740, and returns the time the event was logged (LockedTime), the user account SamAccountName (Account), the source computer (LockedFrom), and the current locked status (Locked)
.Example
@BenNeise
BenNeise / Get-LockedADAccounts.ps1
Last active December 16, 2015 13:29
Searches event logs of domain controllers for event ID 4740, and returns the time the event was logged (LockedTime), the user account SamAccountName (Account), the source computer (LockedFrom), and the current locked status (Locked)
Function Get-LockedADAccounts {
<#
.Synopsis
Gets lockout events from event logs on domain controllers
.Description
Searches event logs of domain controllers for event ID 4740, and returns the time the event was logged (LockedTime), the user account SamAccountName (Account), the source computer (LockedFrom), and the current locked status (Locked)
.Example
@BenNeise
BenNeise / PowerCLIScriptExample1.ps1
Last active January 8, 2017 11:32
Simple example of using PowerCLI
# Get the name of the machine from the user
$strVM = Read-Host "Please enter the VM name"
# Attempt to get a machine-object with that name, continue silently if no machine found
$objectVM = Get-VM -Name $strVM -ErrorAction SilentlyContinue
# If there is a machine found
If ($objectVM) {
# Display the machine object name on screen
Write-Host "Machine object name:" $objectVM.Name
# Display the power state on screen
Write-Host "Power State:" $objectVM.PowerState
@BenNeise
BenNeise / PowerCLIScriptExample2.ps1
Created April 23, 2013 10:45
Find machines where the configured memory is greater than 2000, and filter those machines to display only those with Windows XP guests.
# Loop through each VM
ForEach ($objVM in (
Get-VM | Where-Object {
# Where the configured memory is greater than 2000
$_.MemoryMB -gt "2000"
}
)){
# Get the VM guest object
Get-VMGuest -VM $objVM | Where-Object {
# Where the operating system is Windows XP
@BenNeise
BenNeise / Write-Log.ps1
Created May 9, 2013 13:28
Writes passed string parameters to a logfile, and to screen
Function Write-Log {
<#
.Synopsis
Writes timestamped output to screen and a logfile.
.Description
Writes timestamped output to screen and a logfile.
.Parameter Message
Message to be displayed
@BenNeise
BenNeise / Get-AppSenseLogonTimes.ps1
Created October 28, 2013 14:28
Returns, via the AppSense Event log, the Logon Time, Node Name, Action, Start Time and Duration of AppSense logon actions. Useful for tuning and optimising AppSense logons. The Policy Configuration item "Send events to the Appsense event log" should be enabled for this to work.
Function Get-AppSenseLogonTimes {
<#
.Synopsis
Returns information about AppSense logon events as recorded by the AppSense event log
.Description
Returns, via the AppSense Event log, the Logon Time, Node Name, Action, Start Time and Duration of AppSense logon actions.
Useful for tuning and optimising AppSense logons.
The Policy Configuration item "Send events to the Appsense event log" should be enabled for this to work.
@BenNeise
BenNeise / VMAndGuestNameMismatchFinder.ps1
Created October 29, 2013 12:02
One of the earliest scripts I wrote and felt confident enough to publish on my blog at http://ben.neise.co.uk/index.php/2009/03/vm-object-dns-name-mismatches.
# Get all of the VMs as an object
$objVMs = Get-VM
# Loop through all of the VMs
ForEach ($objVM in $objVMs){
# Get the VM Guest object (which contains the DNS information)
$objGuest = Get-VMGuest -VM $objVM
# Set a variable to the VM object name
$objVMName = $objVM.Name
# Set a variable to the DNS name
$objVMFQDN = $objGuest.Hostname
# Set up an empty array
$arrTemplatesWithPersistentDrives = @()
# Get all the template objects
$objTemplates = Get-Template
# Loop through each template
ForEach ($objTemplate in $objTemplates){
# Get the drives associated with that template
$objHardDisks = $objTemplate | Get-HardDisk