Invoke-Command(icm)
功能:在本地或者远程运行命令,命令执行的结果(含错误)返回到运行命令的机器。
说明:-Computer参数实现在远程运行一条命令。若要运行一系列命令,使用New-PSSesion命令在远程机器创建一个PSSession(持久连接),然后在PSSession中使用Invoke-Command命令中的Session参数去在远程机器运行一系列命令。
实例:
Invoke-Command -FilePath c:\scripts\test.ps1 -ComputerName Server01
Invoke-Command -ComputerName Server01 -Credential Domain01\User01 -ScriptBlock {
Get-Culture }
$ComputerName = "43.76.9.75"
$Username = "libai"
$Password = 'xxxxxx'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
remove-item -Path "c:\remotedirectory\*" -Recurse -Force } -credential $Cred
$version = Invoke-Command -ComputerName (Get-Content myfile.txt) -ScriptBlock {
Get-Culture}
$Log = "PowerShellCore/Operational"
Invoke-Command -ComputerName Server01 -ScriptBlock {
Get-WinEvent -LogName $Using:Log -MaxEvents 10}
$Log = "PowerShellCore/Operational"
$aa="xxxx"
Invoke-Command -ComputerName Server01 -ScriptBlock {
....} -ArgumentList $Log,$aaa
Invoke-Command -ComputerName (Get-Content myfile.txt) -FilePath E:\test.ps1 -ArgumentList Process, Service
Get-Content(cat,gc,type)
功能:获取指定指定位置的对象的内容。
注意:-Delimiter是默认值为\n.对于多行文件,delimiter按照-Delimiter的值来分割文件。所以多行文件返回的是一组对象。
1..100 | ForEach-Object {
Add-Content -Path .\LineNumbers.txt -Value "This is line $_." }
Get-Content -Path .\LineNumbers.txt
Get-Content -Path .\LineNumbers.txt -TotalCount 5
(Get-Content -Path .\LineNumbers.txt -TotalCount 25)[-1]
Get-Item -Path .\LineNumbers.txt | Get-Content -Tail 1
Get-Content -Path E:\* -Filter *.txt
$raw = Get-Content -Path .\LineNumbers.txt -Raw
$lines = Get-Content -Path .\LineNumbers.txt
Write-Host "Raw contains $($raw.Count