For frequently used or important scripts, can be kept to local files, need to be called directly, so it is more convenient to handle!
Script output to a file (if you do not specify an absolute path, the current directory is used by default)
"Get-date" > Myscript.ps1 "pwd" >> myscript.ps1 "' Test '" >> myscript.ps1 "get-date pwd ' Test '" > myscript.ps 1 #或者使用 @ ' @ Bulk write @ ' get-date pwd ' Test ' @ > MYSCRIPT.PS1
Related actions:
#执行文本脚本. \myscript.ps1 #获取脚本到PS控制台 get-content \myscript.ps1 #打开文本文件 notepad myscript.ps1
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010172113532?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
#新建或更新脚本, the script uses the parameter Notepad myscript.ps1 write-host "Hello, $args". \myscript.ps1 KK
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010173401424?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
The parameter usage in the script is the same as the inside definition of the function
#函数 Function test{begin{$i =1} process{$_.name $i + +} end{}} get-service-disp Layname "*mssqlserver*" | Test #脚本 notepad myscript.ps1 begin{$i =1} process{$_.name $i + +} end{}
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010173531427?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
Scripts executed in the pipeline:
#编辑脚本如下 notepad MyScript.ps1 foreach ($element in $input) { if ($element. extension -eq ". exe") { Write-Host -fore "Red" $element. name } else { Write-Host -fore "Green" $element. name } } #或者使用流处理, edit the script as follows notepad myscript.ps1 begin{} process { if ($_. extension -eq ". exe") { Write-Host -fore "Red" $_. name } else { write-host -fore&nbSP; " Green " $_. name } } end{}
#执行 ls $env: windir |. \myscript.ps1
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010173731539?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
Setting aliases is more convenient:
#设置别名更方便 Set-alias edit notepad.exe Edit Myscript.ps1
"Digital Signature Certificate"
Scripts can easily be replaced or higher, and using signature verification tells us whether the script trusts or contains malicious tampering.
To Create a certificate:
Start-All Programs-->microsoft Visual Studio 2012-->visual Studio tools-->vs2012 x86 native Tools command Prompt
Or:
C:\Program Files\Windows Kits\8.1\bin\x86\makecat.exe
Makecert.exe-pe-r-N "cn=powershelltestcert"-eku 1.3.6.1.5.5.7.3.3-ss "My"
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010174011262?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
To view a certificate:
dir cert:\currentuser\my-codesigningcert ls cert:currentuser\my ls cert:currentuser\my | where {$_.subject-eq "Cn=powershelltestcert"}
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010174144230?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
To set up digital signature authentication:
$Cert =ls Cert:currentuser\my | where {$_.subject-eq "Cn=powershelltestcert"} set-authenticodesignature-pspath "Myscript.ps1"-Certificate $Cert Edit Myscript.ps1
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010174323876?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010174332807?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
Signature has been successful!!
Certificate-Related information:
#证书的代表 $Cert. Subject #证书的签发者 $Cert. Issuer #证书的序列号, fingerprint $Cert | Select Serialnumber,thumbprint | FL * #证书是否受信任 $Cert. Verify () #打开证书窗口界面 [System.reflection.assembly]::loadwithpartialname ("System.Security") [ System.Security.Cryptography.x509Certificates.X509Certificate2UI]::D isplaycertificate ($Cert)
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010174546285?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
The certificate opened above is not trusted!
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010174605648?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
Add Trust (or window add: certmgr.msc)
$rootStore = New-object System.security.cryptography.X509Certificates.x509Store ("root", "Currentuser") $ Rootstore.open ("ReadWrite") $rootStore. ADD ($Cert) $rootStore. Close () $Cert. Verify ()
Action Add ($Cert) will prompt the dialog box, click on the confirmation!
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010174755600?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
To add a digital signature to a script:
#给文件 (MYSCRIPT.PS1) Add signature set-authenticodesignature myscript.ps1 $Cert set-authenticodesignature-pspath "MyScript.ps1" -certificate $Cert #给所有文件添加签名 set-authenticodesignature (ls *.ps1) $Cert set-authenticodesignature (Dir-recurse- Include *.ps1) $Cert #文件是否有数字签名 get-authenticodesignature test.ps1 get-authenticodesignature myscript.ps1
For script execution permissions, PowerShell has several settings:
#设置脚本执行权限 set-executionpolicy Restricted #禁止执行脚本 set-executionpolicy Default #默认 set-executionpolicy AllSigned #只执行数字验证的脚本 set-executionpolicy remotesigned #本地无需证书, remote requires certificate Set-executionpolicy unrestricted #无限制, can execute any script
To view the script execution permissions in the current PS:
#查看当前PS中脚本执行权限 Get-executionpolicy Get-executionpolicy-list
The file that is now set to only digitally sign is executable:
#现在设置为数字签名可执行 Set-executionpolicy AllSigned
Execute script (first unsigned, error, second signature, normal execution)
#执行脚本 (first unsigned, error, second signed, normal execution) LS $env: windir |. \test.ps1 get-service-displayname "*mssqlserver*" |. \myscript.ps1
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010175409566?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
Now I tamper with the contents of the file:
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010175959413?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
Execute script, Error!
650) this.width=650; "Src=" http://img.blog.csdn.net/20151010180044778?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
Perfect! ~650) this.width=650; "alt=" Smile "src=" Http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif "/>
This article is from "Alan.dai blog" blogs, declined reprint!
PowerShell Script (Scripts)