Get-ServiceAccounts posh
$administratorsAccount = Get-WmiObject Win32_Group -filter "LocalAccount=True AND SID='S-1-5-32-544'"
$administratorQuery = "GroupComponent = `"Win32_Group.Domain='" + $administratorsAccount.Domain + "',NAME='" + $administratorsAccount.Name + "'`""
$user = Get-WmiObject Win32_GroupUser -filter $administratorQuery | select PartComponent |where {$_ -match $userToFind}
$user
local accts
List the Local Users on a Set of Computers
Submitted by: Robert Cott
List Members of the Local Administrators Group
Submitted by: Anonymous Submission
Add Users to the Local Administrators Group or other local group
Submitted by Jimmy Godard
sub localAccts(strComputer)
Dim objNetwork, objComputer, objUser
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
'Set objComputer = GetObject("WinNT://" & strComputer)
objComputer.Filter = Array("user")
For Each objUser In objComputer
Wscript.Echo objUser.Name & ", " & objUser.Description
Next
end Sub
function Get-ScriptPath{ Split-Path $myInvocation.ScriptName
}
cls
$a = Get-ScriptPath
$a
$b = & { $myInvocation.ScriptName }
$b
$b = & { $myInvocation.ScriptName }
$b
split-path $b
split-path info
# PowerShell's Split-Path can do this for you:
# parent path:
split-path c:\myproject\Csharpproject\memo.txt -Parent
# same as above, w/o the -Parent switch:
split-path c:\myproject\Csharpproject\memo.txt
# file name:
split-path c:\myproject\Csharpproject\memo.txt -Leaf
# ...and more:
split-path c:\myproject\Csharpproject\memo.txt -Qualifier
split-path c:\myproject\Csharpproject\memo.txt -NoQualifier
split-path c:\myproject\Csharpproject\memo.txt -IsAbsolute
Get the Data You Need
split-path info
# PowerShell's Split-Path can do this for you:
# parent path:
split-path c:\myproject\Csharpproject\memo.txt -Parent
# same as above, w/o the -Parent switch:
split-path c:\myproject\Csharpproject\memo.txt
# file name:
split-path c:\myproject\Csharpproject\memo.txt -Leaf
# ...and more:
split-path c:\myproject\Csharpproject\memo.txt -Qualifier
split-path c:\myproject\Csharpproject\memo.txt -NoQualifier
split-path c:\myproject\Csharpproject\memo.txt -IsAbsolute
Get the Data You Need
Select-Properties: a helper for exporting DirectoryEntry objects
Microcode: PowerShell Scripting Tricks: Select-Object (Note Properties) vs Add-Member (Script Properties)
ServerAuditReports
Day 7: Manage Users<
Day 7: Manage Users<
scheduledTasks
performing-networking-tasks-pinging-from-powershell
powershellPing
# This is the key part
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer)
if ($Reply.status –eq “Success”)
{
# do somethine if online
}
else
{
# do somethine if NOT online
}
$Reply = ""
Get-QADComputer -sizeLimit 0 -osName *Server* | Where-Object { (Get-WmiObject Win32_PingStatus -Filter "Address='$($_.DNSHostName)'").StatusCode -eq 0 } | Foreach-Object {
# Do your other work here
}
function Ping-Address {
PROCESS {
$ping = $false
$results = Get-WmiObject -query `
"SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
foreach ($result in $results) {
if ($results.StatusCode -eq 0) {
$ping = $true
}
}
if ($ping -eq $true) {
Write-Output $_
}
}
}
function Restart-Computer {
PROCESS {
$computer = Get-WmiObject Win32_OperatingSystem -computer $_
$computer.Reboot()
}
}