小编use*_*292的帖子

Powershell在控制台和文件中显示消息

不确定这是一个简单的问题,还是需要一些代码来实现这一目标,所以在这里.

我有一个脚本,基本上在PS控制台日志文件中显示一些文本.

我的代码使用了写主机和写输出,所以我的问题是,如果2可以以某种方式组合?我只想记录特定的消息,所以做PS转录本将无法正常工作.

Write-host "Error: whatever message" 
Write-Output "Error: whatever message" | Out-File -Append $Log_file
Run Code Online (Sandbox Code Playgroud)

提前致谢!

powershell powershell-3.0 powershell-4.0

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

循环,直到IP:端口响应

我有一个简单的查询,但test-netConnection或测试连接在循环中使用时给了我很多时间.所以基本上我想在循环中运行,直到服务器响应我在某个端口上的连接尝试.通过测试连接,我发现你无法指定端口,所以我想解决方案是使用tnc - test-netconnection.

在do while循环中它不起作用,因为tnc不使用连续的ping/connect尝试.

do {
  Write-Host "waiting..."
  sleep 3      
} until(Test-NetConnection $HOST -Port PORT)
Run Code Online (Sandbox Code Playgroud)

powershell loops

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

如何使用xml文件作为脚本的参数输入

我有一个 PowerShell 脚本,它必须将一个 xml 文件作为将在脚本中使用的所有参数/变量的输入源。

我目前如何拥有 param xml 输入代码:

param([xml] $xmlData)
Run Code Online (Sandbox Code Playgroud)

但是当脚本执行时,我收到这个错误:

PS> .\script.ps1 -xmlData .\xmlfile.xml
script.ps1 : Cannot process argument transformation on parameter 'xmlData'. Cannot convert
value ".\xmlfile.xml" to type "System.Xml.XmlDocument". Error: "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type."
At line:1 char:22
+ .\script.ps1 -xmlData .\xmlfile.xml
+                      ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [script.ps1], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,script.ps1
Run Code Online (Sandbox Code Playgroud)

在 PS 会话中,如果我这样做它工作正常,我可以看到从 xml 文件解析的节点和数据,所以我不确定应该如何正确完成或者我是否遗漏了什么:

PS> …
Run Code Online (Sandbox Code Playgroud)

xml powershell powershell-3.0

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

标签 统计

powershell ×3

powershell-3.0 ×2

loops ×1

powershell-4.0 ×1

xml ×1