由于最近对Powershell越来越喜欢,在空余时间,整理一下一些常用的经典例子分享给大家:
1. 活动目录,列出域信息
#
===========================================
# 列出域信息
# ===========================================
$strComputer = " . " # zivsoft-lihua
$colItems = get - wmiobject - class " Win32_NTDomain " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Caption: " $objItem .Caption
write - host " Client Site Name: " $objItem .ClientSiteName
write - host " Creation Class Name: " $objItem .CreationClassName
write - host " DC Site Name: " $objItem .DcSiteName
write - host " Description: " $objItem .Description
write - host " DNS Forest Name: " $objItem .DnsForestName
write - host " Domain Controller Address: " $objItem .DomainControllerAddress
write - host " Domain Controller Address Type: " $objItem .DomainControllerAddressType
write - host " Domain Controller Name: " $objItem .DomainControllerName
write - host " Domain GUID: " $objItem .DomainGuid
write - host " Domain Name: " $objItem .DomainName
write - host " DS Directory Service Flag: " $objItem .DSDirectoryServiceFlag
write - host " DS DNS Controller Flag: " $objItem .DSDnsControllerFlag
write - host " DS DNS Domain Flag: " $objItem .DSDnsDomainFlag
write - host " DS DNS Forest Flag: " $objItem .DSDnsForestFlag
write - host " DS Global Catalog Flag: " $objItem .DSGlobalCatalogFlag
write - host " DS Kerberos Distribution Center Flag: " $objItem .DSKerberosDistributionCenterFlag
write - host " DS Primary Domain Controller Flag: " $objItem .DSPrimaryDomainControllerFlag
write - host " DS Time Service Flag: " $objItem .DSTimeServiceFlag
write - host " DS Writable Flag: " $objItem .DSWritableFlag
write - host " Installation Date: " $objItem .InstallDate
write - host " Name: " $objItem .Name
write - host " Name Format: " $objItem .NameFormat
write - host " Primary Owner Contact: " $objItem .PrimaryOwnerContact
write - host " Primary Owner Name: " $objItem .PrimaryOwnerName
write - host " Roles: " $objItem .Roles
write - host " Status: " $objItem .Status
write - host
}
# 列出域信息
# ===========================================
$strComputer = " . " # zivsoft-lihua
$colItems = get - wmiobject - class " Win32_NTDomain " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Caption: " $objItem .Caption
write - host " Client Site Name: " $objItem .ClientSiteName
write - host " Creation Class Name: " $objItem .CreationClassName
write - host " DC Site Name: " $objItem .DcSiteName
write - host " Description: " $objItem .Description
write - host " DNS Forest Name: " $objItem .DnsForestName
write - host " Domain Controller Address: " $objItem .DomainControllerAddress
write - host " Domain Controller Address Type: " $objItem .DomainControllerAddressType
write - host " Domain Controller Name: " $objItem .DomainControllerName
write - host " Domain GUID: " $objItem .DomainGuid
write - host " Domain Name: " $objItem .DomainName
write - host " DS Directory Service Flag: " $objItem .DSDirectoryServiceFlag
write - host " DS DNS Controller Flag: " $objItem .DSDnsControllerFlag
write - host " DS DNS Domain Flag: " $objItem .DSDnsDomainFlag
write - host " DS DNS Forest Flag: " $objItem .DSDnsForestFlag
write - host " DS Global Catalog Flag: " $objItem .DSGlobalCatalogFlag
write - host " DS Kerberos Distribution Center Flag: " $objItem .DSKerberosDistributionCenterFlag
write - host " DS Primary Domain Controller Flag: " $objItem .DSPrimaryDomainControllerFlag
write - host " DS Time Service Flag: " $objItem .DSTimeServiceFlag
write - host " DS Writable Flag: " $objItem .DSWritableFlag
write - host " Installation Date: " $objItem .InstallDate
write - host " Name: " $objItem .Name
write - host " Name Format: " $objItem .NameFormat
write - host " Primary Owner Contact: " $objItem .PrimaryOwnerContact
write - host " Primary Owner Name: " $objItem .PrimaryOwnerName
write - host " Roles: " $objItem .Roles
write - host " Status: " $objItem .Status
write - host
}
2 列出机器安装过的软件列表
#
===================================
# 列出安装列表
# ===================================
$strComputer = " ziz-vmm-lihua " # 可以是局域网远程机器
$colItems = get - wmiobject - class " Win32_Product " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Caption: " $objItem .Caption
write - host " Description: " $objItem .Description
write - host " Identifying Number: " $objItem .IdentifyingNumber
write - host " Installation Date: " $objItem .InstallDate
write - host " Installation Date 2: " $objItem .InstallDate2
write - host " Installation Location: " $objItem .InstallLocation
write - host " Installation State: " $objItem .InstallState
write - host " Name: " $objItem .Name
write - host " Package Cache: " $objItem .PackageCache
write - host " SKU Number: " $objItem .SKUNumber
write - host " Vendor: " $objItem .Vendor
write - host " Version: " $objItem .Version
write - host
}
# 列出安装列表
# ===================================
$strComputer = " ziz-vmm-lihua " # 可以是局域网远程机器
$colItems = get - wmiobject - class " Win32_Product " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Caption: " $objItem .Caption
write - host " Description: " $objItem .Description
write - host " Identifying Number: " $objItem .IdentifyingNumber
write - host " Installation Date: " $objItem .InstallDate
write - host " Installation Date 2: " $objItem .InstallDate2
write - host " Installation Location: " $objItem .InstallLocation
write - host " Installation State: " $objItem .InstallState
write - host " Name: " $objItem .Name
write - host " Package Cache: " $objItem .PackageCache
write - host " SKU Number: " $objItem .SKUNumber
write - host " Vendor: " $objItem .Vendor
write - host " Version: " $objItem .Version
write - host
}
3. 列出制定计算机共享文件夹信息
#
===========================================
# 列出共享文件夹信息
# ===========================================
$strComputer = " ziz-vmm-lihua " # "."
$colItems = get - wmiobject - class " Win32_Share " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Access Mask: " $objItem .AccessMask
write - host " Allow Maximum: " $objItem .AllowMaximum
write - host " Caption: " $objItem .Caption
write - host " Description: " $objItem .Description
write - host " Installation Date: " $objItem .InstallDate
write - host " Maximum Allowed: " $objItem .MaximumAllowed
write - host " Name: " $objItem .Name
write - host " Path: " $objItem .Path
write - host " Status: " $objItem .Status
write - host " Type: " $objItem .Type
write - host
}
# 列出共享文件夹信息
# ===========================================
$strComputer = " ziz-vmm-lihua " # "."
$colItems = get - wmiobject - class " Win32_Share " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Access Mask: " $objItem .AccessMask
write - host " Allow Maximum: " $objItem .AllowMaximum
write - host " Caption: " $objItem .Caption
write - host " Description: " $objItem .Description
write - host " Installation Date: " $objItem .InstallDate
write - host " Maximum Allowed: " $objItem .MaximumAllowed
write - host " Name: " $objItem .Name
write - host " Path: " $objItem .Path
write - host " Status: " $objItem .Status
write - host " Type: " $objItem .Type
write - host
}
4.列出一台机器里所有文件夹信息
$strComputer
=
"
.
"
#
也可以是远程机器如zivsoft-lihua
$colItems = get - wmiobject - class " Win32_Directory " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Access Mask: " $objItem .AccessMask
write - host " Archive: " $objItem .Archive
write - host " Caption: " $objItem .Caption
write - host " Compressed: " $objItem .Compressed
write - host " Compression Method: " $objItem .CompressionMethod
write - host " Creation Class Name: " $objItem .CreationClassName
write - host " Creation Date: " $objItem .CreationDate
write - host " CS Creation Class Name: " $objItem .CSCreationClassName
write - host " CS Name: " $objItem .CSName
write - host " Description: " $objItem .Description
write - host " Drive: " $objItem .Drive
write - host " 8.3 File Name: " $objItem .EightDotThreeFileName
write - host " Encrypted: " $objItem .Encrypted
write - host " Encryption Method: " $objItem .EncryptionMethod
write - host " Extension: " $objItem .Extension
write - host " File Name: " $objItem .FileName
write - host " File Size: " $objItem .FileSize
write - host " File Type: " $objItem .FileType
write - host " FS Creation Class Name: " $objItem .FSCreationClassName
write - host " FS Name: " $objItem .FSName
write - host " Hidden: " $objItem .Hidden
write - host " InstallationDate: " $objItem .InstallDate
write - host " In Use Count: " $objItem .InUseCount
write - host " Last Accessed: " $objItem .LastAccessed
write - host " Last Modified: " $objItem .LastModified
write - host " Name: " $objItem .Name
write - host " Path: " $objItem .Path
write - host " Readable: " $objItem .Readable
write - host " Status: " $objItem .Status
write - host " System: " $objItem .System
write - host " Writeable: " $objItem .Writeable
write - host
}
$colItems = get - wmiobject - class " Win32_Directory " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Access Mask: " $objItem .AccessMask
write - host " Archive: " $objItem .Archive
write - host " Caption: " $objItem .Caption
write - host " Compressed: " $objItem .Compressed
write - host " Compression Method: " $objItem .CompressionMethod
write - host " Creation Class Name: " $objItem .CreationClassName
write - host " Creation Date: " $objItem .CreationDate
write - host " CS Creation Class Name: " $objItem .CSCreationClassName
write - host " CS Name: " $objItem .CSName
write - host " Description: " $objItem .Description
write - host " Drive: " $objItem .Drive
write - host " 8.3 File Name: " $objItem .EightDotThreeFileName
write - host " Encrypted: " $objItem .Encrypted
write - host " Encryption Method: " $objItem .EncryptionMethod
write - host " Extension: " $objItem .Extension
write - host " File Name: " $objItem .FileName
write - host " File Size: " $objItem .FileSize
write - host " File Type: " $objItem .FileType
write - host " FS Creation Class Name: " $objItem .FSCreationClassName
write - host " FS Name: " $objItem .FSName
write - host " Hidden: " $objItem .Hidden
write - host " InstallationDate: " $objItem .InstallDate
write - host " In Use Count: " $objItem .InUseCount
write - host " Last Accessed: " $objItem .LastAccessed
write - host " Last Modified: " $objItem .LastModified
write - host " Name: " $objItem .Name
write - host " Path: " $objItem .Path
write - host " Readable: " $objItem .Readable
write - host " Status: " $objItem .Status
write - host " System: " $objItem .System
write - host " Writeable: " $objItem .Writeable
write - host
}
5. 列出指定机器中的所有Scheduled Tasks列表
$strComputer
=
"
zivosft-lihua-pc
"
$colItems = get - wmiobject - class " Win32_ScheduledJob " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Caption: " $objItem .Caption
write - host " Command: " $objItem .Command
write - host " Days Of Month: " $objItem .DaysOfMonth
write - host " Days Of Week: " $objItem .DaysOfWeek
write - host " Description: " $objItem .Description
write - host " Elapsed Time: " $objItem .ElapsedTime
write - host " Installation Date: " $objItem .InstallDate
write - host " Interact With Desktop: " $objItem .InteractWithDesktop
write - host " Job ID: " $objItem .JobId
write - host " Job Status: " $objItem .JobStatus
write - host " Name: " $objItem .Name
write - host " Notify: " $objItem .Notify
write - host " Owner: " $objItem .Owner
write - host " Priority: " $objItem .Priority
write - host " Run Repeatedly: " $objItem .RunRepeatedly
write - host " Start Time: " $objItem .StartTime
write - host " Status: " $objItem .Status
write - host " Time Submitted: " $objItem .TimeSubmitted
write - host " Until Time: " $objItem .UntilTime
write - host
}
$colItems = get - wmiobject - class " Win32_ScheduledJob " - namespace " root/CIMV2 " `
- computername $strComputer
foreach ( $objItem in $colItems ) {
write - host " Caption: " $objItem .Caption
write - host " Command: " $objItem .Command
write - host " Days Of Month: " $objItem .DaysOfMonth
write - host " Days Of Week: " $objItem .DaysOfWeek
write - host " Description: " $objItem .Description
write - host " Elapsed Time: " $objItem .ElapsedTime
write - host " Installation Date: " $objItem .InstallDate
write - host " Interact With Desktop: " $objItem .InteractWithDesktop
write - host " Job ID: " $objItem .JobId
write - host " Job Status: " $objItem .JobStatus
write - host " Name: " $objItem .Name
write - host " Notify: " $objItem .Notify
write - host " Owner: " $objItem .Owner
write - host " Priority: " $objItem .Priority
write - host " Run Repeatedly: " $objItem .RunRepeatedly
write - host " Start Time: " $objItem .StartTime
write - host " Status: " $objItem .Status
write - host " Time Submitted: " $objItem .TimeSubmitted
write - host " Until Time: " $objItem .UntilTime
write - host
}
6. 一个动态输出自定义cmdlets帮助信息与Examples的函数,帮助你学习陌生的cmdlets
#
======================================================
# Usage: Updated by Lihua at 05/05/2009 zivsoft.com
# OutputHelp SqlServerCmdletSnapin100 SqlCmdletsFolder
# OutputHelp HelloWorldPSSnapin HelloFolder
# Output UnknownPSSnapIn UnknownCmdletsFolder
# ======================================================
function OutputHelp{
$snapin = $Args [ 0 ] # the snapin that you will specified
# verify parameter
if ( $snapin -eq $null ){
throw " The first parameter Snapin is null "
}
$folder = $Args [ 1 ] # the folder where files will stored
$snapins = Get - PSSnapin - Name $snapin
if ( $snapins -eq $null ){
# add pssnapin
Add - PSSnapin $snapin
}
$d = Get - Command - PSSnapin $snapin
# verify the existing snapin has the command-let
if ( $d -eq $null ){
throw " The snapin $snapin has none cmdlets "
}
" Total: " + $d .Count
$i = 0
# Verify the folder exists
if ([System.IO.Directory] :: Exists([AppDomain] :: CurrentDomain.BaseDirectory.ToString() + $folder ) -eq $false ){
md $folder
}
$d | foreach - process {
$i ++
" Printing " + $i
$cmdlet = $_ .Name
$path = " ./$folder/ " + $cmdlet + " .ps1 "
Get - Help - Examples $cmdlet > $path
}
" Successfully "
}
# Usage: Updated by Lihua at 05/05/2009 zivsoft.com
# OutputHelp SqlServerCmdletSnapin100 SqlCmdletsFolder
# OutputHelp HelloWorldPSSnapin HelloFolder
# Output UnknownPSSnapIn UnknownCmdletsFolder
# ======================================================
function OutputHelp{
$snapin = $Args [ 0 ] # the snapin that you will specified
# verify parameter
if ( $snapin -eq $null ){
throw " The first parameter Snapin is null "
}
$folder = $Args [ 1 ] # the folder where files will stored
$snapins = Get - PSSnapin - Name $snapin
if ( $snapins -eq $null ){
# add pssnapin
Add - PSSnapin $snapin
}
$d = Get - Command - PSSnapin $snapin
# verify the existing snapin has the command-let
if ( $d -eq $null ){
throw " The snapin $snapin has none cmdlets "
}
" Total: " + $d .Count
$i = 0
# Verify the folder exists
if ([System.IO.Directory] :: Exists([AppDomain] :: CurrentDomain.BaseDirectory.ToString() + $folder ) -eq $false ){
md $folder
}
$d | foreach - process {
$i ++
" Printing " + $i
$cmdlet = $_ .Name
$path = " ./$folder/ " + $cmdlet + " .ps1 "
Get - Help - Examples $cmdlet > $path
}
" Successfully "
}
7. 关闭(重启)远程计算机
#
=================================
# 在有管理员权限下关闭远程计算机
# Copyright http://www.zivsoft.com
# =================================
function ShutdownRemoteMachine{
param ([string] $machineName = $null )
$os = Get - WmiObject Win32_OperatingSystem - computerName $machineName
# shutdown /m //MACHINENAME /r /f
$os .Win32Shutdown( 6 , 0 ) # 远程关机
}
# 在有管理员权限下关闭远程计算机
# Copyright http://www.zivsoft.com
# =================================
function ShutdownRemoteMachine{
param ([string] $machineName = $null )
$os = Get - WmiObject Win32_OperatingSystem - computerName $machineName
# shutdown /m //MACHINENAME /r /f
$os .Win32Shutdown( 6 , 0 ) # 远程关机
}
文章连接:http://www.cnblogs.com/architect/archive/2009/05/05/1450050.html
作者相关:http://www.zivsoft.com