# path to a digitally signed file (adjust path to an existing signed ps1 file): $Path = "$env:temp\test.ps1" $Status = Get-AuthenticodeSignature-FilePath$Path | Select-Object-Property * $Status
结果类似这样:
SignerCertificate : [Subject]
CN=MyPowerShellCode
[Issuer]
CN=MyPowerShellCode
[Serial Number]
1B98E986A1D7BCB245034A0225381CA4
[Not Before]
01.05.2022 17:39:56
[Not After]
01.05.2027 17:49:56
[Thumbprint]
F4C1F9978D564E143D554F3679746B3A79E1FF87
TimeStamperCertificate :
Status : UnknownError
StatusMessage : A certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider
Path : C:\Users\tobia\AppData\Local\Temp\test.ps1
SignatureType : Authenticode
IsOSBinary : False
# digitally sign this file (adjust path to an existing ps1 file): $Path = "$env:temp\test.ps1"
# adjust this path to point to a valid code signing certificate: $CertPath = 'Cert:\CurrentUser\my\F4C1F9978D564E143D554F3679746B3A79E1FF87'
# if it does not exist, create a dummy file $exists = Test-Path-Path$Path if ($exists-eq$false) { 'Hello World!' | Set-Content-Path$Path-Encoding UTF8 }
# read a code signing certificate to use for signing: $myCert = Get-Item-Path$CertPath
# add a digital signature to a PS script file: Set-AuthenticodeSignature-FilePath$Path-Certificate$myCert
# show changes inside script file: notepad $Path
运行此代码时,在 $Path 中指定的脚本文件将打开并显示添加到脚本底部的数字签名:
Hello World!
# SIG # Begin signature block
# MIIFcAYJKoZIhvcNAQcCoIIFYTCCBV0CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU4QK+x7NLicgrIdzN+Nvxqbuq
# Qv2gggMKMIIDBjCCAe6gAwIBAgIQG5jphqHXvLJFA0oCJTgcpDANBgkqhkiG9w0B
...
# hiding errors: $ErrorActionPreference = 'SilentlyContinue' # telling all cmdlets to use a private variable for error logging: $PSDefaultParameterValues.Add('*:ErrorVariable', '+myErrors') # initializing the variable: $myErrors = $null
# do stuff: Stop-Service-Name Spooler dir c:\gibtsnichtabc
# check errors at end USING PRIVATE VARIABLE: $myErrors.Count $myErrors | Out-GridView
# the ID you picked when saving the options: $id = 5388
# the name of the reg value that stores your choices: $flag = "StateFlags$id"
# the location where user choices are stored: $path = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*"
# get all subkeys where your user choice was enabled: Get-Item-Path$path | Where-Object { $_.Property -contains$flag } # contains your registry value (StateFlags5388) Where-Object { $_.GetValue($flag) -gt0 } # your registry value contains a number greater than 0
Get-PnpDevice-Class Bluetooth | Where-Object HardwareID -match'DEV_' | Select-Object FriendlyName, $Address | Where-Object Address | Out-GridView-Title'Select Bluetooth Device to Remove'-OutputMode Single
functionUnpair-Bluetooth { # take a UInt64 either directly or as part of an object with a property # named "DeviceAddress" or "Address" param ( [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias('Address')] [UInt64] $DeviceAddress )
# tell PowerShell the location of the internal Windows API # and define a static helper function named "Unpair" that takes care # of creating the needed arguments: begin { Add-Type-Namespace"Devices"-Name'Bluetooth'-MemberDefinition' [DllImport("BluetoothAPIs.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.U4)] static extern UInt32 BluetoothRemoveDevice(IntPtr pAddress); public static UInt32 Unpair(UInt64 BTAddress) { GCHandle pinnedAddr = GCHandle.Alloc(BTAddress, GCHandleType.Pinned); IntPtr pAddress = pinnedAddr.AddrOfPinnedObject(); UInt32 result = BluetoothRemoveDevice(pAddress); pinnedAddr.Free(); return result; }' }
# do this for every object that was piped into this function: process { $result = [Devices.Bluetooth]::Unpair($DeviceAddress) [PSCustomObject]@{ Success = $result-eq0 ReturnValue = $result } } }
Status ClassFriendlyNameInstanceId ------ ----- ------------ ---------- OKBluetoothBoseQC35IIAvrcpTransportBTHENUM\{0000110C-00... OK Bluetooth Generic Attribute Profile BTHLEDEVICE\{0000180... OK Bluetooth Generic Access Profile BTHLEDEVICE\{0000180... OK Bluetooth Generic Attribute Profile BTHLEDEVICE\{0000180... OK Bluetooth Bamboo Ink Plus BTHLE\DEV_006FF2E608... OK Bluetooth Bose QC35 II Avrcp Transport BTHENUM\{0000110E-00... OK Bluetooth Generic Attribute Profile BTHLEDEVICE\{0000180... OK Bluetooth SMA001d SN: 2110109033 SN2110109033 BTHENUM\DEV_0080251B... OK Bluetooth MX Master 3 BTHLE\DEV_D304DEE615... OK Bluetooth Bluetooth LE Generic Attribute Service BTHLEDEVICE\{0000FE5... OK Bluetooth Microsoft Bluetooth LE Enumerator BTH\MS_BTHLE\6&1B2C8... OK Bluetooth Device Information Service BTHLEDEVICE\{0000180... OK Bluetooth Bluetooth LE Generic Attribute Service BTHLEDEVICE\{0000180... OK Bluetooth Device Information Service BTHLEDEVICE\{0000180... OK Bluetooth Generic Access Profile BTHLEDEVICE\{0000180... OK Bluetooth Bluetooth LE Generic Attribute Service BTHLEDEVICE\{0001000... OK Bluetooth Bluetooth LE Generic Attribute Service BTHLEDEVICE\{0000180... OK Bluetooth MX Keys BTHLE\DEV_D9FDB81EAB... OK Bluetooth Device Information Service BTHLEDEVICE\{0000180... OK Bluetooth Microsoft Bluetooth Enumerator BTH\MS_BTHBRB\6&1B2C... OK Bluetooth Generic Access Profile BTHLEDEVICE\{0000180... OK Bluetooth Bluetooth LE Generic Attribute Service BTHLEDEVICE\{0001000... OK Bluetooth Intel(R) Wireless Bluetooth(R) USB\VID_8087&PID_002... OK Bluetooth Bluetooth Device (RFCOMM Protocol TDI) BTH\MS_RFCOMM\6&1B2C... OK Bluetooth Bluetooth LE Generic Attribute Service BTHLEDEVICE\{0000180... OK Bluetooth Bose QC35 II BTHENUM\DEV_2811A579...
要根据名称搜索特定的蓝牙设备,请尝试下面一行代码。它会查找名称中带有 “Bose” 的所有设备:
1 2 3 4 5 6 7
PS> Get-PnpDevice-Class Bluetooth -FriendlyName *Bose*
Status ClassFriendlyName ------ ----- ------------ OKBluetoothBoseQC35IIAvrcpTransport OKBluetoothBoseQC35IIAvrcpTransport OKBluetoothBoseQC35II
PS> Get-PnpDevice-Class Bluetooth | Select-Object-Property Caption, Manufacturer, Service
Caption Manufacturer Service -------------------------- Bose QC35 II Avrcp Transport Microsoft Microsoft_Bluetooth_AvrcpTransport Generic Attribute Profile Microsoft UmPass Generic Access Profile Microsoft UmPass Generic Attribute Profile Microsoft UmPass Bamboo Ink Plus Microsoft BthLEEnum Bose QC35 II Avrcp Transport Microsoft Microsoft_Bluetooth_AvrcpTransport Generic Attribute Profile Microsoft UmPass SMA001d SN: 2110109033 SN2110109033 Microsoft MX Master 3 Microsoft BthLEEnum Bluetooth LE Generic Attribute Service Microsoft UmPass Microsoft Bluetooth LE Enumerator Microsoft BthLEEnum Device Information Service Microsoft UmPass Bluetooth LE Generic Attribute Service Microsoft UmPass Device Information Service Microsoft UmPass Generic Access Profile Microsoft UmPass Bluetooth LE Generic Attribute Service Microsoft UmPass Bluetooth LE Generic Attribute Service Microsoft UmPass MX Keys Microsoft BthLEEnum Device Information Service Microsoft UmPass Microsoft Bluetooth Enumerator Microsoft BthEnum Generic Access Profile Microsoft UmPass Bluetooth LE Generic Attribute Service Microsoft UmPass Intel(R) Wireless Bluetooth(R) Intel Corporation BTHUSB Bluetooth Device (RFCOMM Protocol TDI) Microsoft RFCOMM Bluetooth LE Generic Attribute Service Microsoft UmPass Bose QC35 II Microsoft
当您使用名词 “PnPDevice” 搜索其他 cmdlet 时,还可以发现启用或禁用的命令:
1 2 3 4 5 6 7
PS> Get-Command-Noun PnPDevice
CommandType Name Version Source ---------------------------- FunctionDisable-PnpDevice1.0.0.0PnpDevice FunctionEnable-PnpDevice1.0.0.0PnpDevice FunctionGet-PnpDevice1.0.0.0PnpDevice