Search This Blog

Friday, March 25, 2011

Useful PS Commands


Start Auto Services
--------------------
Get-WmiObject win32_service | where{ ($_.StartMode -eq "Auto") -and ($_.State -eq "Stopped")} | foreach{"Starting " + $_.DisplayName + " ..." ;$b=$_.StartService()}

Get-WmiObject -computer win32_service | where{ ($_.StartMode -eq "Auto") -and ($_.State -eq "Stopped")} | foreach{"Starting " + $_.DisplayName + " ..." ;$b=$_.StartService()}

Find Dependent Services
-------------------------
Get-WmiObject win32_dependentservice | foreach{"="*50;"Service:";"-"*25;[wmi]$_.Dependent| Ft DisplayName -hide ;"Dependency:";"-"*25;[wmi]$_.Antecedent  | Ft Displayname -hide}

Get Eventlog
--------------
get-EventLog system -newest 2000 | where {$_.entryType -match "Error"}

Get Specific event
-------------------
get-eventlog system | where{$_.Message -match "Bluetooth"}

Get last 10 errors
-------------------
get-eventlog system | where{$_.entrytype -eq "Error"} | select -first 10

Summarize last 1000 events
--------------------------
Get-eventlog -logname system -newest 1000 | group-object -property source -noelement | sort-object -property count -descending

Get Event Log Summary since last boot
--------------------------------------
Get-eventlog system | Where{ ($_.TimeWritten -ge [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime))} | Group-object EntryType |Sort-Object Name |Format-Table Name, Count -Autosize

Get Event Log Summary in last 24 hours
-----------------------------------------
Get-eventlog system | Where{ ($_.TimeWritten -ge (get-date).AddDays(-1))} | Group-object EntryType |Sort-Object Name |Format-Table Name, Count -Autosize

Get system event Error since last boot
-------------------------------------
get-eventlog system | Where{ ($_.TimeWritten -ge [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime)) -and ($_.EntryType -eq "Error")}

Get Error in last 24 hours
------------------------------
Get-eventlog system | Where{ ($_.TimeWritten -ge (get-date).AddDays(-1)) -and ($_.EntryType -eq "Error")}

Reboot-After Crash
-------------------
Get-EventLog system | where{$_.eventid -eq 6008} |ft  >> systemhealth.txt

System-uptime
------------------
powershell (get-date) - (Get-WmiObject -Class Win32_OperatingSystem).ConvertToDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime) | fl Days,Hours,Minutes  >> c:\systemhealth.txt

Boot Time
-----------
[Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime)

Disk Space
-------------------
Get-WmiObject win32_logicaldisk -filter "DriveType=3"| ft deviceid, @{Name="Size";Expression={"{0:N2}" -f($_.size/1gb) + " GB"}} , @{Name="FreeSpace";Expression={"{0:N2}" -f($_.freespace/1gb) + " GB" }}, @{Name="in %";Expression={"{0:N2}" -f($_.freespace/$_.size*100) + "%" }} -auto

Get-WmiObject win32_logicaldisk -filter "DriveType=3"| ft @{Label="Drive";expression={$_.deviceid}}, @{label="Freespace";expression={"{0:P2}" -f($_.freespace/[int64]$_.size)}} -auto

Find Process owner
-----------------
Get-WmiObject win32_Process | foreach{ process{ $_.Name + "`t" + $_.getOwner().user }}

Physical Network Adapter Status
-------------------------------
Get-WmiObject win32_networkadapter -filter "PhysicalAdapter='True'" | Format-Object NetConnectionID, NetConnectionStatus , AdapterType  , Name  -AutoSize

IP Configuration for Connected network
---------------------------------------
Get-WmiObject -Class win32_networkadapter -filter "netconnectionstatus = 2" | foreach-object { Get-WmiObject -Class win32_networkadapterconfiguration  -filter "Index = $($_.deviceID)"} | Format-Table IPAddress, IPSubnet, DefaultIPGateway, DHCPEnabled , DHCPServer , DNSHostName -Autosize

Get-WmiObject -Class win32_networkadapter -filter "netconnectionstatus = 2" | foreach-object {$a= $_.NetConnectionId; Get-WmiObject -Class win32_networkadapterconfiguration  -filter "Index = $($_.deviceID)" } | Format-Table  @{Name="Name";Expression={"$a"}},IPAddress, IPSubnet, DefaultIPGateway, DHCPEnabled , DHCPServer , DNSHostName -Autosize


Assign IP to Network Adapter
---------------------------
$NetAdapter = Get-WmiObject win32_networkadapter -filter "NetConnectionID='External NIC'" | foreach {Get-WmiObject win32_networkadapterconfiguration -filter "Index = $($_.DeviceID)"}

$NetAdapter.EnableStatic("10.10.10.10","255.255.255.0")
$NetAdapter.SetGateways("10.10.10.1",1)

Set DNS
----------
$NetAdapter.SetDNSServerSearchOrder("10.10.10.1")

Enable DHCP
------------
$NetAdapter.EnableDHCP()

Search a file in use
-------------------
Get-Process |foreach{$_.Name}; select ProcessName -Expandproperty Modules | where{$_.ModuleName -match "PowerShell.Exe"} | FT ModuleName, Filename

Get Time server from registry
-------------------------------

([WMIClass]"\root\default:stdregprov").GetStringValue(2147483650,"SYSTEM\Currentcontrolset\services\w32time\parameters","ntpserver").svalue

User account
--------------
$guest = ([ADSI]"WinNT://kzx/guest")
$guest.Description = "Guest Account"

$EnableUser = 512
$DisableUser = 2
$guest.Userflags  = $Disableuser

$guest.SetInfo()


([ADSI]"WinNT://ksofts/hema").SetPassword("345")

$ou = [ADSI]"WinNT://hqf001"
$ou = [ADSI]"WinNT://ksofts"
$u = $ou.Create("User","Heekaa")
$u.setpassword("123")
$u.setinfo()

$u = $ou.create("Group", "ServerAdmin")
$u.setinfo()

Uninstall Powershell
--------------------

(Get-WmiObject -class Win32_Product -filter "Name='Windows PowerShell(TM) V2 (CTP3)'").uninstall()

To Find VSS Issue
------------------

Powershell get-eventlog application | where{$_.entrytype -eq "Error" -and $_.source -eq "VSS"}

Top Ten Memory Eaters
----------------------

foreach($p in (Get-Process|Sort ws -des |select -f 10)){$ws="{0:N2}" -f ($p.WorkingSet/1MB); Write-Host $p.Name =  $ws MB}

Find disk usage
---------------

foreach($drive in (Get-WmiObject -Class win32_logicaldisk -filter "drivetype='3'")){$fs="{0:P2}" -f ($drive.freespace/$drive.size);$drive.Name+"="+ $fs}

------------------------------------------
Powershell {
$mylog = "c:\temp\log" + (Get-date -uformat %d%m%Y)+".txt";
Get-EventLog system | where{$_.eventid -eq 20} >> $mylog;
Get-EventLog system | where{$_.eventid -eq 8193} >> $mylog;
notepad $mylog;
}

Powershell {
$mylog = "c:\temp\log" + (Get-date -uformat %d%m%Y)+".txt";
Get-EventLog system | where{$_.eventid -eq 6008} >> $mylog;
notepad $mylog;
}



foreach($p in ($gp=Get-ProcesS)){$tp+=$p.TotalProcessorTime.TotalSeconds;};foreach($i in $gp){$i.name + "=" + "{0:P0}" -f ($i.TotalProcessorTime.TotalSeconds/$tp)}

foreach($p in ($gp=Get-ProcesS)){$tp+=$p.TotalProcessorTime.TotalSeconds;};foreach($i in $gp){($i.TotalProcessorTime.TotalSeconds/$tp)}

gc srv.txt | foreach {(Get-WmiObject win32_service -computer $_ -filter "name = 'TapiSrv'").ChangeStartMode("Manual")}
gc srv.txt | foreach {Get-WmiObject win32_service -computer $_ -filter "name = 'TapiSrv'" | ft StartMode}
gc s2.txt |foreach{$comp=$_;Get-WmiObject win32_service -computer $comp | where{ ($_.StartMode -eq "Auto") -and ($_.State -eq "Stopped")} | foreach{$Comp+":"+"Starting " +$_.DisplayName + " ..." ;$b=$_.StartService()}}
gc srv.txt | %{gwmi win32_service -computer $_ -filter "name='cqmghost'"| ft SystemName,state}

gc s2.txt | %{$_; REG QUERY \\$_\HKEY_LOCAL_MACHINE\Software\Nestle\Computer\Hotfix\ }

REG QUERY \\USPHXA0006\HKEY_LOCAL_MACHINE\Software\Nestle\Computer\Hotfix\ | ?{$_ -match "O3220-124"}

http://windowsitpro.com/article/articleid/100845/resolve-wmi-problems-quickly-with-wmidiag.html

get-eventlog -LogName oasis | ? {$_.EntryType -eq "Error" -and $_.TimeWritten -ge (get-date).AddDays(-1)}

Find DNS settings
=================
gc srv.txt | %{$_ >> olddns.txt; get-wmiobject -computer $_ Win32_NetworkAdapterConfiguration -Filter "IPEnabled=1" |%{$_.DnsServerSearchOrder >> olddns.txt}}
gc srv.txt | %{$_ >> newdns.txt; get-wmiobject -computer $_ Win32_NetworkAdapterConfiguration -Filter "IPEnabled=1" |%{$_.DnsServerSearchOrder >> newdns.txt}}
$a = "10.112.30.7","10.112.48.155"
get-wmiobject Win32_NetworkAdapterConfiguration -Filter "IPEnabled=1" | %{$_.SetDnsServerSearchOrder($a)}

TimeZone
===================
Get-WmiObject -Class win32_TimeZone -computer HQVEVB0008, HQVEVM0011, HQVEVM0012, HQVEVM0013  | Ft  __SERVER, Caption -au

No comments:

Post a Comment

Followers