我想在PowerShell中逐行读取文件.具体来说,我想循环遍历文件,将每一行存储在循环中的变量中,并在该行上进行一些处理.
我知道Bash的等价物:
while read line do
if [[ $line =~ $regex ]]; then
# work here
fi
done < file.txt
Run Code Online (Sandbox Code Playgroud)
关于PowerShell循环的文档不多.
使用Windows命令提示符,我可以回显%path%并在单独的行上获取结果路径吗?像这样的东西,但对于Windows:
echo $path | tr ':' '\n'
Run Code Online (Sandbox Code Playgroud)
我可以使用vanilla cmd执行此操作,还是需要PowerShell或js脚本?
示例echo%path%output:
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;
Run Code Online (Sandbox Code Playgroud)
期望的输出:
C:\WINDOWS\system32;
C:\WINDOWS;
C:\WINDOWS\System32\Wbem;
C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;
C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;
C:\Program Files\Microsoft SQL Server\90\Tools\binn\;
C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;
Run Code Online (Sandbox Code Playgroud) 如何编写PowerShell脚本是否有任何推荐的编码风格?
它不是关于如何构造代码(有多少函数,如果使用模块,......).它是关于" 如何编写代码以使其可读 ".
在编程语言中有一些推荐的编码样式(缩进的内容,如何缩进 - 空格/制表符,在哪里创建新行,在哪里放括号,...),但我还没有看到任何关于PowerShell的建议.
我特别感兴趣的是:
如何写参数
function New-XYZItem
( [string] $ItemName
, [scriptblock] $definition
) { ...
Run Code Online (Sandbox Code Playgroud)
(我看到它更像'V1'语法)
要么
function New-PSClass {
param([string] $ClassName
,[scriptblock] $definition
)...
Run Code Online (Sandbox Code Playgroud)
或(为什么要添加空属性?)
function New-PSClass {
param([Parameter()][string] $ClassName
,[Parameter()][scriptblock] $definition
)...
Run Code Online (Sandbox Code Playgroud)
或(我在Jaykul的代码中看到的其他格式)
function New-PSClass {
param(
[Parameter()]
[string]
$ClassName
,
[Parameter()]
[scriptblock]
$definition
)...
Run Code Online (Sandbox Code Playgroud)
要么 ...?
如何编写复杂的管道
Get-SomeData -param1 abc -param2 xyz | % {
$temp1 = $_
1..100 | % { …Run Code Online (Sandbox Code Playgroud) 使用Windows PowerShell,如何更改命令提示符?
例如,默认提示符说
PS C:\Documents and Settings\govendes\My Documents>
Run Code Online (Sandbox Code Playgroud)
我想自定义该字符串.
在x64位OS平台上运行PowerShell脚本时,如何在脚本中确定脚本运行的PowerShell(32位或64位)版本?
背景
默认情况下,32位和64位版本的PowerShell都安装在64位平台(如Windows Server 2008)上.这可能会导致运行必须针对特定体系结构的PowerShell脚本时遇到困难(即使用64-用于SharePoint 2010脚本的位,以便使用64位库).
相关问题:
是否可以使用post build事件设置.net项目来执行powershell脚本?我正在使用此脚本生成一些文件.我也可以通过它是脚本的调试版本还是发布版本.这方面的一个例子很棒.
例如,当我的PowerShell脚本尝试为不存在的服务器创建SQL Server对象时(在我的情况下为"bla"),PowerShell会以红色显示许多PowerShell错误.
由于我的脚本会检查$?此类调用之后的值,并显示和记录错误,因此我不想显示多行PowerShell错误.
如何停用为脚本显示的内容?
所以,如果我有一个存储在变量中的目录,请说:
$scriptPath = (Get-ScriptDirectory);
Run Code Online (Sandbox Code Playgroud)
现在我想找到两个父级别的目录.
我需要一个很好的做法:
$parentPath = Split-Path -parent $scriptPath
$rootPath = Split-Path -parent $parentPath
Run Code Online (Sandbox Code Playgroud)
我可以在一行代码中找到rootPath吗?
我正在尝试编写一个可以获得管道输入的PowerShell脚本(并且预计会这样做),但尝试类似的东西
ForEach-Object {
# do something
}
Run Code Online (Sandbox Code Playgroud)
在使用命令行中的脚本时实际上不起作用,如下所示:
1..20 | .\test.ps1
Run Code Online (Sandbox Code Playgroud)
有办法吗?
注意:我了解功能和过滤器.这不是我想要的.
如果资源不存在,以下PowerShell脚本将起作用.
Copy-Item $src_dir $dst_dir$agent_folder -recurse
Run Code Online (Sandbox Code Playgroud)
但如果有资源,它会说:
+ Copy-Item <<<< $src_dir $dst_dir$agent_folder -recurse
+ CategoryInfo : ResourceExists: (C:\Users\Pac\Desktop\Agents\Agent0\lib:S
tring) [Copy-Item], IOException
+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.CopyItemComm
and
Run Code Online (Sandbox Code Playgroud)
我有什么要添加到命令,以便它将无条件地复制文件?