Powershell绝对不是我的“强项”之一,但每隔一段时间,我都会尝试编写一些脚本来自动执行手动任务。无论如何,这是我的问题所在。该脚本按原样工作,并将导出csv文件并按主机名头排序。我的问题是,有没有更好的方法来做这件事?
#Clear the output screen
cls
#set the working directory to where the script and text files are located (must be same directory)
$scriptdir = Split-Path -Parent $PSCommandPath
cd $script
我让这段代码与powershell.Invoke()同步工作得很好,但是使用powershell.BeginInvoke()我不能捕获输出。要使用下面的代码,您需要填充$servers变量,否则它应该运行。
我试图通过在使用EndInvoke()时将每个线程添加到$threads变量来捕获输出,并且我能够看到线程句柄和iscompleted值,但是我不能确定我在每个函数的返回部分中返回的值存储在哪里。
第一个块是我看到的输出,其中一个异步正在完成,它显示为false,直到它完成,然后所有线程句柄都显示为true。
谢谢!
8804 is True
16420 is True
13352 is
我的Powershell脚本正在抛出一个异常,但没有给出抛出异常的行号:
The process cannot access the file 'C:/path/to/foo.txt' because it is being used by another process.
+ CategoryInfo : NotSpecified: (:) [Set-Content], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.SetCo
试图简化URL健康检查PowerShell脚本中的日志记录。它的工作方式是我想要的,除了Start-Transcript给出的输出过于冗长和重复本身。基于此,默认的调试/详细首选项设置为SilentlyContinue at initialSessionState,这将阻止输出到主机。但它显示为脚本输出,这是我试图消除的行为。
脚本:
#Place URL list file in the below path
$URLListFile = "C:\Users\Admin\Documents\Scripts\URL Check\URL_Check.txt"
$URLList =
我在PowerShell阅读线路上遇到了一个问题。我的要求是:
使用select字符串,如果找到一个模式,则将行读到“。将在一行中显示输出。。
我的档案看起来是这样的:
file: this is the first
file for
read.
error:this is the error.
data: this is the data.
file: this is the
second file.
在该文件中,如果找到" file“,则将整行读入”下一步“。被发现了。因为行被截断了。
期望的产出是:
file: this is the first file to