#实例化对象,以SqlConnection为例子
$con
=
new
-
object System.Data.Sqlclient.SqlConnection(
"
server=.;uid=xxx;pwd=xxx
"
)
$strMachineName
=
[System.Environment]
::
MachineName
$result = [System.Math] :: Pow( 2 , 3 )
$result = [System.Math] :: Pow( 2 , 3 )
#列出以sql开头的进程
get
-
process
sql
*
#列出所有进程,按照占用的workingset大小降序排列
get
-
process
|
sort
-
object workingset
-
desceding
#列出本机共享
get
-
wmiobject win32_share
#得到实际的cpu个数
get
-
wmiobject
-
query
"
select *from Win32_Processor
"
#如何使用if else,foreach , 得到所有的服务,安装服务状态进行排序 1
$serviceArray=get-service | sort-object status -descending
foreach ($temp in $serviceArray)
{
if($temp.status -eq "stopped")
{
write-host "The" $temp.name "is stopped" -foreground red
}
elseif($temp.status -eq "running")
{
write-host "The" $temp.name "is running" -foreground green
}
else
{
write-host "The" $temp.name "status is " $temp.status -foreground yellow
}
}
foreach ($temp in $serviceArray)
{
if($temp.status -eq "stopped")
{
write-host "The" $temp.name "is stopped" -foreground red
}
elseif($temp.status -eq "running")
{
write-host "The" $temp.name "is running" -foreground green
}
else
{
write-host "The" $temp.name "status is " $temp.status -foreground yellow
}
}
#如何使用if else,foreach , 得到所有的服务,安装服务状态进行排序 2
get
-
service
|
sort
-
object status
-
descending
|
foreach {
if ( $_ .status -eq " stopped " )
{
write - host " The " $_ .name " is stopped " - foreground red
}
elseif ( $_ .status -eq " running " )
{
write - host " The " $_ .name " is running " - foreground green
}
else
{
write - host " The " $_ .name " status is " $_ .status - foreground yellow
}
}
foreach {
if ( $_ .status -eq " stopped " )
{
write - host " The " $_ .name " is stopped " - foreground red
}
elseif ( $_ .status -eq " running " )
{
write - host " The " $_ .name " is running " - foreground green
}
else
{
write - host " The " $_ .name " status is " $_ .status - foreground yellow
}
}
#如何使用switch , 得到所有的服务,安装服务状态进行排序
get
-
service
|
sort
-
object status
-
descending
|
foreach {
switch ( $_ .status)
{
" stopped "
{
write - host " The " $_ .name " is stopped " - foreground red
}
" running "
{
write - host " The " $_ .name " is running " - foreground green
}
default
{
write - host " The " $_ .name " status is " $_ .status - foreground yellow
}
}
}
foreach {
switch ( $_ .status)
{
" stopped "
{
write - host " The " $_ .name " is stopped " - foreground red
}
" running "
{
write - host " The " $_ .name " is running " - foreground green
}
default
{
write - host " The " $_ .name " status is " $_ .status - foreground yellow
}
}
}
#switch 通配符,统计出包含“sql”关键字的字符串数量
$counter
=
0
$stringArray = " sqlserver " , " t-sql " , " serviceBroker " , " TDE " |
foreach {
switch -wildcard ( $_ )
{
" *sql* "
{
$counter ++
}
default
{
write - host $_
}
}
}
Write - host " there are " $counter " terms contain sql "
$stringArray = " sqlserver " , " t-sql " , " serviceBroker " , " TDE " |
foreach {
switch -wildcard ( $_ )
{
" *sql* "
{
$counter ++
}
default
{
write - host $_
}
}
}
Write - host " there are " $counter " terms contain sql "
#switch 正则表达式
$stringArray
=
"
sqlserver
"
,
"
t-sql
"
,
"
serviceBroker
"
,
"
TDE
"
,
"
The version of sqlserver is 2008, and the edition is enterprise
"
|
foreach {
switch -regex ( $_ )
{
" sql.*?\d+ " # 包含sql,并且后面至少有一个数字
{
write - host $_ - foreground red
}
default
{
write - host $_
}
}
}
foreach {
switch -regex ( $_ )
{
" sql.*?\d+ " # 包含sql,并且后面至少有一个数字
{
write - host $_ - foreground red
}
default
{
write - host $_
}
}
}
#得到状态为"运行"的服务 get-service
$strState
=
"
running
"
get - service | where - object { $_ .status -eq $strState }
get - service | where - object { $_ .status -eq $strState }
#得到状态为"运行"的服务 get-wmiobject
$strState
=
"
running
"
get - wmiobject win32_service - Filter " state='$strState' "
get - wmiobject win32_service - Filter " state='$strState' "
#将内容写入文件, out-file cmdlet
$strState
=
"
running
"
$strPath = " c:\runningService.txt "
get - service | where - object { $_ .status -eq $strState } | out -file - filepath $strPath
$strPath = " c:\runningService.txt "
get - service | where - object { $_ .status -eq $strState } | out -file - filepath $strPath
#查找字符串
param
(
$folder
=
"
C:\TEMP
"
,
$keywords
=
"
*xyz*
"
,
$extension
=
"
.txt
"
)
$files = get - childitem $folder - recurse | where { $_ .extension -eq $extension } |
foreach ( $_ ){
$fileFullName = $_ .FullName
$content = get - content $fileFullName
switch -wildcard ( $content )
{
$keywords
{
write - host $fileFullName
}
default
{
# write-host $content
}
}
}
$files = get - childitem $folder - recurse | where { $_ .extension -eq $extension } |
foreach ( $_ ){
$fileFullName = $_ .FullName
$content = get - content $fileFullName
switch -wildcard ( $content )
{
$keywords
{
write - host $fileFullName
}
default
{
# write-host $content
}
}
}