使用证书签名PowerShell脚本


1.创建自签名证书(如需要)

PS C:\Windows\system32> New-SelfSignedCertificate -DnsName www.mycard.com -CertStoreLocation Cert:\CurrentUser\My


    目录: Microsoft.PowerShell.Security\Certificate::CurrentUser\My


Thumbprint                                Subject
----------                                -------
313C6D95807E577BBD62D3F0A31077C911B72EB6  CN=www.mycard.com


2.获取证书

    a).查找证书存储位置

PS C:\Windows\system32> Get-PSDrive

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
Alias                                  Alias
C                  44.32         16.86 FileSystem    C:\
Cert                                   Certificate   \
PS C:\Windows\system32> dir Cert:\CurrentUser\My -OutVariable x


    目录: Microsoft.PowerShell.Security\Certificate::CurrentUser\My


Thumbprint                                Subject
----------                                -------
C744543541A7B9E5E0085F38351727765A716415  CN=domain\user
313C6D95807E577BBD62D3F0A31077C911B72EB6  CN=www.mycard.com
PS C:\Windows\system32> $x[0]


    目录: Microsoft.PowerShell.Security\Certificate::CurrentUser\My


Thumbprint                                Subject
----------                                -------
C744543541A7B9E5E0085F38351727765A716415  CN=domain\user

3.脚本签名前后

PS C:\Windows\system32> cat E:\1.ps1
Get-Variable
PS C:\Windows\system32> Set-AuthenticodeSignature -Certificate $x[0] -FilePath 'E:\1.ps1'


    目录: E:\


SignerCertificate                         Status                                 Path
-----------------                         ------                                 ----
FBD0E44E1011C4C992CD5004D73137A3EC0E5AE4  Valid                                  1.ps1
PS C:\Windows\system32> cat E:\1.ps1
Get-Variable
# SIG # Begin signature block
# MIIHfgYJKoZIhvcNAQcCoIIHbzCCB2sCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUFo/Es9HHBQSTlqXXU6T/dWK2
# l7KgggV6MIIFdjCCBF6gAwIBAgIKE3B1nwAAAABmQDANBgkqhkiG9w0BAQUFADB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hvcNAQkEMRYEFHwOQIGZoxKLrIov7cZ1wBhrMA57
# MA0GCSqGSIb3DQEBAQUABIGAEmp+D25Uxjxuof3PzPuOagbWurKjYYnds8ThTCuj
# lswYU6DMnXyp37HLbl0tvNMYnXk71GUX/p4KC/EN9RDUWtipZUrCtydwlfPQPEAw
# KGaz0eKD0h2TAwLliTcHRM+WhjgSur2N5Or4z3EXCca8mJjesVZJj5eZfNPK97sX
# TeU=
# SIG # End signature block




你可能感兴趣的:(powershell,脚本签名)