小编Dom*_*tti的帖子

在PowerShell中运行openssl命令

我在PowerShell中执行以下命令:

Invoke-Expression "openssl pkcs12 -in $certCN.pfx -nocerts -nodes -out $certCN.key -password pass:1111"
Run Code Online (Sandbox Code Playgroud)

它工作正常,但输出openssl导致丑陋的控制台错误:

openssl : MAC verified OK
At line:1 char:1
+ openssl pkcs12 -in www.mywebsite.com.pfx -nocerts -node ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MAC verified OK:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
Run Code Online (Sandbox Code Playgroud)

执行此OpenSSL命令并忽略输出或至少不将其解释为命令的最佳方法是什么?

windows powershell command

6
推荐指数
1
解决办法
1万
查看次数

按X退出或任何其他键继续?

Grrr ...... Noob在这里提问.我想创建一个基本上这样做的块:

Write-Host "Press X to cancel or any other key to continue"
$continue = Read-Host
  If ($continue = "X") 
     {exit}
  else 
     {Write-Host "Hello world"}
Run Code Online (Sandbox Code Playgroud)

即使我按下另一把钥匙也要退出...我做错了什么?谢谢!!!

powershell

4
推荐指数
1
解决办法
5081
查看次数

从 powershell 中的本地计算机证书存储中删除证书?

我正在尝试检查证书,然后将其删除(如果该证书存在于用户的本地计算机存储中)。我试过这个:

$certCN = 'test.domain.com'
Set-Location Cert:\LocalMachine\My
$oldCert = Get-ChildItem -Recurse | 
Where-Object { $_.subject -like "CN=$oldCert*" }
Remove-Item Cert:\LocalMachine\My\$oldCert -Force
Run Code Online (Sandbox Code Playgroud)

但它并没有从商店中删除证书或给出任何错误(是的,我正在运行此提升)。

我检查了我的$oldCert变量以查看它是否已填充,它是:

PS Cert:\LocalMachine\My> $oldcert
PSParentPath: Microsoft.PowerShell.Security\Certificate::LocalMachine\My

Thumbprint                                Subject                                                                                                                                                                       
----------                                -------                                                                                                                                                                       
276B7B87740D5E9595A258060F5CD9CC4190E9E1  CN=test.domain.com, <truncated>
Run Code Online (Sandbox Code Playgroud)

有谁知道如何做到这一点?对此,我真的非常感激。

powershell

4
推荐指数
1
解决办法
8891
查看次数

从Powershell中的Get-WebBinding获取字符串格式的IIS IP地址

我正在尝试通过ID(与名称)检索我引用的网站的IP地址。我能弄清楚的最好方法是在“ bindingInformation”属性上使用正则表达式,如下所示:

$siteID = "22"
$website = Get-Website | Where { $_.ID -eq $siteID }
$iP = Get-WebBinding $website.name | Where { $_.bindingInformation -match "/[^:]*/" }
Run Code Online (Sandbox Code Playgroud)

但是,它似乎没有填充$ iP变量吗?

当我逐步执行时,得到以下信息:

PS IIS:\sites> Get-WebBinding $website.name

protocol  bindingInformation                                                                        
-------- ------------------                                                                        
http      10.206.138.131:80:                                                                        
http      10.206.138.131:80:dev1.RESERVED22                                                         
http      10.206.138.131:80:dev1.www.RESERVED22 
Run Code Online (Sandbox Code Playgroud)

我猜我不确定如何将$ _。bindingInformation转换为字符串格式变量?对于Powershell来说还很陌生,如果这看起来很简单,请抱歉。在此示例中,我需要将$ IP变量设置为“ 10.206.138.131” ...谢谢您的帮助。

iis powershell

3
推荐指数
1
解决办法
2771
查看次数

检查帐户是否是本地组的成员,并在powershell 2.0中执行IF/ELSE

我想知道如何最好地接近这一点.基本上我有一个脚本需要检查USER1是否是本地管理员的成员,如果是,请删除它.这些组都是本地的,脚本将在我需要检查的系统上运行(不需要远程处理).

我正在思考捕获和评估输出的方法

net localgroup Administrators

test\user1
test\user2
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何捕获输出以进行评估(对PowerShell来说很新).有没有人做过这样的事情?我非常感谢任何帮助.

powershell

2
推荐指数
1
解决办法
2万
查看次数

以数组的最后N个元素代替PowerShell中的第一个元素

快速提问。我有以下几点:

$domain = "my.new.domain.com"
$domain.Split('.')[0,1]
Run Code Online (Sandbox Code Playgroud)

...返回值:

my
new
Run Code Online (Sandbox Code Playgroud)

那太好了,除了我需要最后两个(domain.com),并且不确定如何做到这一点。不幸的是,拆分的数量是可变的(例如test.my.new.domain.com)。一个人怎么说“到底并计数X向后分裂”?

powershell

2
推荐指数
1
解决办法
4864
查看次数

如果Powershell中的/ Else没有按预期运行?

有人可以向我解释为什么下面没有按预期工作?在下面的例子中,我希望$shortFQDN变量被设置为test.com,但我回来了www.test.com:

$fqdn = 'dev.www.test.com'
$d3 = $fqdn.Split('.')[-3] #www
$d2 = $fqdn.Split('.')[-2] #website
$d1 = $fqdn.Split('.')[-1] #com

#Check if this is a 2LD domain (.com.au, .co.uk, .co.jp, etc.)
if ($d1 -ne 'com' -or 'net' -or 'org' -or 'edu' -or 'gov' -or 'biz') {
    $shortFQDN = -join ("$d3",'.',"$d2",'.',"$d1")
} else {
    $shortFQDN = -join ("$d2",'.',"$d1")
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?似乎if/else处理不正确...

powershell

2
推荐指数
2
解决办法
80
查看次数

如何测试PowerShell中的系统上是否存在程序/可执行文件?

我有一个需要在系统上安装 openssl 的脚本。我想检查一下它是否已安装。我已经考虑过使用,test-path但是因为这个脚本将在许多计算机上,所以无法知道用户安装 openssl 的位置或者它是否在系统路径中。

有没有办法做类似的事情test-command openssl(我知道那不存在)并获得错误级别或类似的信息以在 powershell 中返回?

非常感谢!

powershell

2
推荐指数
1
解决办法
248
查看次数

将未知数量的值传递给 PowerShell 脚本参数?

我熟悉如何从命令行接受参数或参数并将它们传递给 PowerShell:

powershell.exe -file myscript.ps1 -COMPUTER server1 -DATA abcd
Run Code Online (Sandbox Code Playgroud)
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True)]
    [string]$computer,

    [Parameter(Mandatory=$True)]
    [string]$data
)
Run Code Online (Sandbox Code Playgroud)

这很好,但是如果$computer参数不止一个项目并且项目数量未知怎么办?例如:

Powershell.exe -file myscript.ps1 -COMPUTER server1, server2, server3 -DATA abcd
Run Code Online (Sandbox Code Playgroud)

这里我们不知道$computer会有多少项目。总会有一个,但也可能有 2、3、4 等。这样的事情如何才能最好地实现?

powershell parameter-passing

1
推荐指数
1
解决办法
2021
查看次数

在foreach循环中分批暂停?

我正在编写一个脚本,将500多个DNS记录迁移到Windows 2008(从03开始),然后操纵属性-全部通过DNSCMD(内置MS DNS工具)。

无论如何-DNS中存在一个局限性,如果您“做得太快”,它就会开始阻塞自身-我以前在旧的DOS脚本中已经看到了这一点。

也就是说,我将如何批处理一个foreach循环?

例如(用外行术语加上脚本)

$records = C:\myfile.txt

foreach ($record in records) {
     MyFunction (only to the first 20 records)
     start-sleep 2
     MyFunction (to the next 20 records)
... etc .... 
    }
Run Code Online (Sandbox Code Playgroud)

那可能吗?如果是这样,您将如何处理?我想我可以添加一个简单的start-sleep 1after MyFunction,但是具有500多个记录,是许多函数的乘积,这将花费很长时间。:(就我们从DOS世界中发现的效率而言,批处理它们是最好的。谢谢!

powershell

0
推荐指数
1
解决办法
486
查看次数

$ _之间有什么区别?和$ _

不是程序员也不是脚本人,但我很快就开始使用Powershell,并且热爱它.那就是说,当你说的时候有什么不同

$_.(something) and    
$_ (something)
Run Code Online (Sandbox Code Playgroud)

powershell

0
推荐指数
1
解决办法
2955
查看次数

标签 统计

powershell ×11

command ×1

iis ×1

parameter-passing ×1

windows ×1