zhang-snail

导航

 

(0)powershell常用命令

Power shell官方学习文档https://learn.microsoft.com/en-us/powershell/scripting/how-to-use-docs?view=powershell-7.4

学习博主参照1https://www.cnblogs.com/Ulysse/tag/PowerShell/

学习博主参照2https://www.cnblogs.com/cqpanda/category/2145620.html

1.PowerShell判断数组里面是否包含指定字符串元素


判断数组是否包含指定元素
PS C:\WINDOWS\system32> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
PS C:\WINDOWS\system32> $arrColors -contains "black"
False

判断数组是否包含指定元素(大小写敏感)
PS C:\WINDOWS\system32> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
PS C:\WINDOWS\system32> $arrColors -ccontains "Green"
False

判断数组是否不包含指定元素
PS C:\WINDOWS\system32> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
PS C:\WINDOWS\system32> $arrColors -notcontains "violet"
True

判断数组是否不包含指定元素(大小写敏感)
PS C:\WINDOWS\system32> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
PS C:\WINDOWS\system32> $arrColors -cnotcontains "Green"
True

2.PowerShell:连接与分隔字符串

一、连接字符串

  • 使用"+"连接字符串:将字符串连接在一起,字符串之间没有分隔符。
PS C:\WINDOWS\system32> $string1="abc" 
PS C:\WINDOWS\system32> $string2="def"
PS C:\WINDOWS\system32> $string3=$string1+$string2
PS C:\WINDOWS\system32> $string3
abcdef

  • "-Join"连接字符串:

语法:

-Join (String1,String2,String3...) 不使用分隔符连接字符串

String1,String2,String3… -Join "Delimiter" 使用分隔符连接字符串

例1:

PS C:\WINDOWS\system32> $a=-Join("abc","def","ghi")
PS C:\WINDOWS\system32> $a
abcdefghi

例2:

PS C:\WINDOWS\system32> $b="abc","def","ghi" -Join ":"
PS C:\WINDOWS\system32> $b
abc:def:ghi
  • 使用"*"运算符:字符串自连接
PS C:\WINDOWS\system32> $string1="abc"
PS C:\WINDOWS\system32> $string2=$string1*3
PS C:\WINDOWS\system32> $string2
abcabcabc

二、分隔字符串

语法:

-Split String 根据空格分隔字符串

String -Split "Delimiter" [,MaxSubStrings] 根据指定分隔符分隔字符串

例1:

PS C:\WINDOWS\system32> $a="abc def ghi"
PS C:\WINDOWS\system32> -Split $a
abc
def
ghi

例2:

PS C:\WINDOWS\system32> $a="abc:def:ghi"
PS C:\WINDOWS\system32> $a -Split ":"
abc
def
ghi

3.PowerShell中的灵活的参数验证功能

1)普通方式:

PS C:\WINDOWS\system32> function Get-Weekday
{
  param
  (
    $Weekday
  )
  "You chose $Weekday"
}

PS C:\WINDOWS\system32> Get-Weekday -Weekday NoWeekday
You chose NoWeekday

2).正则表达式类型的验证方式:
一旦用户输入的字符串与你指定的模式不匹配时,Powershell会抛出一个异常,但是这个异常信息不够友好。在输出参数时,控制台或着ISE编辑器也不能智能提示:

PS C:\WINDOWS\system32> function Get-Weekday
{
param
(
[ValidatePattern('Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday')]
$Weekday
)
"You chose $Weekday"
}

PS C:\WINDOWS\system32> Get-Weekday -Weekday NoWeekday
Get-Weekday : Cannot validate argument on parameter 'Weekday'. The argument "NoWeekday" does not match the "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday" pattern. Supply an argument that matches "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday
|Sunday" and try the command again.
At line:1 char:22
+ Get-Weekday -Weekday NoWeekday
+                      ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Weekday], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-Weekday
 

3)使用ValidateSet:
用户在输出参数时被限定在你规定的值集合中,另外在ISE中还会智能提示用户允许的值列表。

PS C:\WINDOWS\system32> function Get-Weekday
{
param
(
[ValidateSet('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')]
$Weekday
)
"You chose $Weekday"
}

PS C:\WINDOWS\system32> Get-Weekday -Weekday NoWeekday
Get-Weekday : Cannot validate argument on parameter 'Weekday'. The argument "NoWeekday" does not belong to the set "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" specified by the ValidateSet attribute. Supply an argument that is in the set and t
hen try the command again.
At line:1 char:22
+ Get-Weekday -Weekday NoWeekday
+                      ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Weekday], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-Weekday
 

4.查看、执行服务命令

1)查看windows下的所有服务

Get-Service 或者 sc.exe query

2)查看运行中的服务

Get-Service |findstr "Running"

3)查看某个服务状态

Get-Service -ServiceName Dhcp 或者 sc.exe query Dhcp

4)启动某个服务

sc.exe start Dhcp

5)停止某个服务

sc.exe stop Dhcp


5.write-error、write-warning、$host.UI.WriteErrorLine('内容')、$host.UI.WriteErrorLine('内容')

如果你想在PowerShell控制台上输出错误信息或者警告信息,可以分别使用write-error和write-warning这两条命令。它们会采用系统中默认的字体颜色来打印文本。PowerShell会为你的输出采用一个文本模板。

PS C:\WINDOWS\system32> Write-Warning 'Hello,world!'
WARNING: Hello,world!

PS C:\WINDOWS\system32> Write-Error 'You are a big bad egg~'
Write-Error 'You are a big bad egg~' : You are a big bad egg~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

但是Write-Error会引入一个实际的异常,可能会中断脚本的继续运行。如果你只想输出一个看起来像错误的消息,那不妨试试下面这种靠谱一点的写法:
PS C:\WINDOWS\system32> $Host.UI.WriteErrorLine('你是个小坏蛋~')
你是个小坏蛋~

PS C:\WINDOWS\system32> $Host.UI.WriteWarningLine('你是个大坏蛋~')
WARNING: 你是个大坏蛋~

6.文件或文件夹操作

1)删除文件夹及其下所有文件

Remove-Item D:/uu898work/log -Force -Recurse

2)复制文件

Copy-Item d:\ops\win\nssm.exe c:\windows\system32\

posted on 2024-01-18 19:15  zhang-snail  阅读(20)  评论(0编辑  收藏  举报