PowerShell 脚本(Scripts)

对于经常用或者重要的脚本,可以保持到本地文件中,需要的时候可直接调用,这样处理更加方便!

 

编写脚本输出到文件 (若不指定绝对路径,默认都使用当前目录)

"Get-Date" > MyScript.ps1
"pwd" >> MyScript.ps1
"'测试'" >> MyScript.ps1


"Get-Date
pwd
'测试'" > MyScript.ps1


#或者使用 @''@ 批量写入
@'
Get-Date
pwd
"测试"
'@ > MyScript.ps1


相关操作:

#执行文本脚本
.\MyScript.ps1


#获取脚本到PS控制台
Get-Content .\MyScript.ps1


#打开文本文件
notepad MyScript.ps1

 

#新建或更新脚本,脚本中使用参数
notepad MyScript.ps1

Write-Host "Hello,$args"

.\MyScript.ps1 kk

 

 

 

脚本中的参数用法与函数内部定义一样

#函数
Function Test{    
begin{ $i=1 }    
process{    
     $_.name  
     $i++    
}    
end{}    
}    
  
Get-Service -DisplayName "*MSSQLSERVER*" | Test  



#脚本 notepad MyScript.ps1
begin{ $i=1 }    
process{    
     $_.name  
     $i++    
}    
end{} 

 

 

 

管道中执行的脚本:

#编辑脚本如下 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
    }
}



#或者使用流处理,编辑脚本如下 notepad MyScript.ps1
begin{}
process
{
    if($_.Extension -eq ".exe")
    {
        Write-Host -fore "red" $_.Name
    }
    else
    {
        Write-Host -fore "Green" $_.Name
    }
}
end{}
#执行
ls $env:windir | .\MyScript.ps1

 

 

设置别名更方便:

 

 

 

#设置别名更方便
Set-Alias edit notepad.exe

edit MyScript.ps1

 

脚本中传递变量:

param (
	[Parameter(ValueFromPipeline = $true, HelpMessage = "Enter your name: ")]
	[String]$Name = "kk" #default value
)

Write-Host "Your name is $Name."
Write-Host ("Your name is {0} and {1}." -f $Name,"mm")

 

 

执行:

 

【数字签名证书】

脚本很容易被替换或者更高,使用签名验证会告诉我们脚本是否信任,是否包含了恶意篡改。

 

创建证书:
开始——>所有程序——>Microsoft Visual Studio 2012——>Visual Studio Tools——>VS2012 x86 本机工具命令提示
或者:
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"

 

查看证书:

dir Cert:\CurrentUser\My -CodeSigningCert
ls cert:CurrentUser\My
ls cert:CurrentUser\My | where {$_.subject -eq "CN=PowerShellTestCert"}

 

 

 

设置数字签名认证:

$Cert=ls cert:CurrentUser\My | where {$_.subject -eq "CN=PowerShellTestCert"}
Set-AuthenticodeSignature -PSPath "MyScript.ps1" -Certificate $Cert

edit MyScript.ps1


 

 

签名已经成功!!

 

证书相关信息:

#证书的代表
$Cert.subject

#证书的签发者
$Cert.issuer

#证书的序列号,指纹
$Cert | select SerialNumber,Thumbprint | fl *

#证书是否受信任
$Cert.Verify()

#打开证书窗口界面
[System.Reflection.Assembly]::LoadWithPartialName("System.Security")
[System.Security.Cryptography.x509Certificates.X509Certificate2UI]::DisplayCertificate($Cert)

 

 

 

上面打开的证书并不受信任!

 

添加信任(或者窗口添加:certmgr.msc)

$rootStore= New-Object system.security.cryptography.X509Certificates.x509Store("root","Currentuser")
$rootStore.Open("ReadWrite")
$rootStore.Add($Cert)
$rootStore.Close()
$Cert.Verify()


操作 Add($Cert) 会提示对话框,点击确认即可!

 

 

给脚本添加数字签名:

#给文件(MyScript.ps1)添加签名
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


对于脚本的执行权限,powershell 有几个设置:

#设置脚本执行权限
Set-ExecutionPolicy Restricted		#禁止执行脚本
Set-ExecutionPolicy Default		#默认
Set-ExecutionPolicy AllSigned		#只执行数字验证的脚本
Set-ExecutionPolicy RemoteSigned	#本地无需证书,远程需要证书
Set-ExecutionPolicy Unrestricted	#无限制,可执行任何脚本


查看当前PS中脚本执行权限:

 

#查看当前PS中脚本执行权限
Get-ExecutionPolicy
Get-ExecutionPolicy -List


现在设置为只有数字签名的文件可执行:

#现在设置为数字签名可执行
Set-ExecutionPolicy AllSigned


执行脚本(第一个无签名,报错;第二个有签名,正常执行)

 

#执行脚本(第一个无签名,报错;第二个有签名,正常执行)
ls $env:windir | .\test.ps1
Get-Service -DisplayName "*MSSQLSERVER*" | .\MyScript.ps1

 

 

 

现在我把文件内容篡改:

 

执行脚本,出错!

完美!~微笑

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中文名: PowerShell脚本编写手册 原名: Windows Powershell Scripting Guide 作者: Ed Wilson 资源格式: PDF 版本: 影印版 出版社: Microsoft Press书号: 073562279发行时间: 2008年 地区: 美国 语言: 英文 内容简介: Get practical guidance for using Windows PowerShell to manage Windows Vista and Windows Server 2008. Written by Ed Wilson, a leading scripting expert and trainer at Microsoft, this reference offers a task-based approach to help you find the information you need for day-to-day tasks. With more than 200 scripts, it offers rich examples that administrators can customize for their own environment and needs. The scripts range in complexity from one-line commands, to full-blown scripts with managed output and command-line arguments examples that are applicable to all skill levels. Includes a companion CD with fully searchable eBook, sample scripts, and other resources for managing your Windows-based environment.Key Book Benefits Delivers more than 200 scripts administrators can customize and use to get up and running quickly Provides multiple ways of accomplishing tasks: from one-line commands to full-blown scripts with managed output and command-line arguments Features a task-oriented approach, and organized to help you quickly find the information you need for your day-to-day activities Includes a companion CD with a fully searchable eBook, sample scripts, and other resources for on-the-job results 目录: 1. The Shell in Windows Powershell. 2. Scripting Windows Powsershell 3. Managing Logs 4. Managing Services 5. Managing Shares 6. Managing Printing 7. Desktop Maintenance 8. Networking 9. Configuration Desktop Settings 10. Managing Post-Deployment Issues 11. Managing User Data 12. Troubleshooting Windows 13. Managing Domain Users 14. Configuring the Cluster Service 15. Managing Internet Information Services 16. Working with the Certificate Store 17. Managing the Terminal Services Service 18. Configuring Network Services 19. Working with Windows Server 2008 Server Core

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值