标签: powershell

如何使用PowerShell删除文件上的ReadOnly属性?

如何使用PowerShell(版本1.0)脚本删除文件上的ReadOnly属性?

powershell scripting powershell-1.0

97
推荐指数
5
解决办法
7万
查看次数

为什么我的PowerShell脚本没有运行?

我写了一个简单的批处理文件作为PowerShell脚本,我在运行时遇到错误.

它位于我路径的脚本目录中.

__PRE__

我查看了帮助,但这并没有帮助.

powershell

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

如何使用PowerShell替换文件中的多个字符串

我正在编写一个用于自定义配置文件的脚本.我想在这个文件中替换多个字符串实例,我尝试使用PowerShell来完成这项工作.

它适用于单个替换,但执行多次替换非常慢,因为每次它必须再次解析整个文件,并且此文件非常大.该脚本如下所示:

$original_file = 'path\filename.abc'
$destination_file =  'path\filename.abc.new'
(Get-Content $original_file) | Foreach-Object {
    $_ -replace 'something1', 'something1new'
    } | Set-Content $destination_file
Run Code Online (Sandbox Code Playgroud)

我想要这样的东西,但我不知道如何写它:

$original_file = 'path\filename.abc'
$destination_file =  'path\filename.abc.new'
(Get-Content $original_file) | Foreach-Object {
    $_ -replace 'something1', 'something1aa'
    $_ -replace 'something2', 'something2bb'
    $_ -replace 'something3', 'something3cc'
    $_ -replace 'something4', 'something4dd'
    $_ -replace 'something5', 'something5dsf'
    $_ -replace 'something6', 'something6dfsfds'
    } | Set-Content $destination_file
Run Code Online (Sandbox Code Playgroud)

powershell replace

96
推荐指数
5
解决办法
16万
查看次数

使用Start-Process捕获标准输出和错误

Start-Process访问StandardErrorStandardOutput属性时,PowerShell 命令中是否存在错误?

如果我运行以下命令,我得不到输出:

$process = Start-Process -FilePath ping -ArgumentList localhost -NoNewWindow -PassThru -Wait
$process.StandardOutput
$process.StandardError
Run Code Online (Sandbox Code Playgroud)

但是,如果我将输出重定向到文件,我得到预期的结果:

$process = Start-Process -FilePath ping -ArgumentList localhost -NoNewWindow -PassThru -Wait -RedirectStandardOutput stdout.txt -RedirectStandardError stderr.txt
Run Code Online (Sandbox Code Playgroud)

powershell start-process

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

如何在Powershell中做头,尾,多,少,多做?

我需要这些命令来检查Windows上的日志文件,但我没有安装任何程序,我喜欢Powershell与Windows.

windows powershell

96
推荐指数
3
解决办法
10万
查看次数

如何从命令提示符将布尔值传递给PowerShell脚本

我必须从批处理文件中调用PowerShell脚本.脚本的一个参数是布尔值:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -File .\RunScript.ps1 -Turn 1 -Unify $false
Run Code Online (Sandbox Code Playgroud)

该命令失败,并显示以下错误:

Cannot process argument transformation on parameter 'Unify'. Cannot convert value "System.String" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.

At line:0 char:1
+  <<<< <br/>
+ CategoryInfo          : InvalidData: (:) [RunScript.ps1], ParentContainsErrorRecordException <br/>
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,RunScript.ps1
Run Code Online (Sandbox Code Playgroud)

截至目前,我在我的脚本中使用字符串进行布尔转换.但是如何将布尔参数传递给PowerShell?

powershell scripting

93
推荐指数
7
解决办法
15万
查看次数

PowerShell脚本不接受$(美元)符号

我正在尝试使用PowerShell脚本打开SQL数据连接,我的密码包含一个$符号:

$cn = new-object system.data.SqlClient.SqlConnection("Data Source=DBNAME;Initial Catalog=Catagory;User ID=User;Password=pass$word;")
Run Code Online (Sandbox Code Playgroud)

当我尝试打开连接时,它说:

登录失败

powershell escaping

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

PowerShell中"%"(百分比)的作用是什么?

似乎%操作在管道之后启动脚本块,尽管about_Script_Blocks表示%不是必需的.

这一切都很好.

get-childitem | %{ write-host $_.Name }

{ write-host 'hello' }

%{ write-host 'hello' }
Run Code Online (Sandbox Code Playgroud)

但是当我们在管道之后添加一个脚本块时,我们需要首先使用%.

get-childitem | { write-host $_.Name }
Run Code Online (Sandbox Code Playgroud)

syntax powershell

92
推荐指数
3
解决办法
7万
查看次数

使用命令行参数从C#执行PowerShell脚本

我需要在C#中执行PowerShell脚本.该脚本需要命令行参数.

这是我到目前为止所做的:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(scriptFile);

// Execute PowerShell script
results = pipeline.Invoke();
Run Code Online (Sandbox Code Playgroud)

scriptFile包含类似"C:\ Program Files\MyProgram\Whatever.ps1"的内容.

该脚本使用命令行参数,例如"-key Value",而Value可以是类似于也可能包含空格的路径.

我不这样做.有谁知道如何从C#中将命令行参数传递给PowerShell脚本并确保空格没有问题?

c# powershell scripting command-line arguments

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

如何在调用Add-PSSnapin之前检查是否已加载PowerShell管理单元

我有一组PowerShell脚本有时可以一起运行,有时一次运行.每个脚本都要求加载某个管理单元.

现在每个脚本都Add-PSSnapin XYZ在开头调用.

现在,如果我背靠背地运行多个脚本,则后续脚本会抛出:

无法添加Windows PowerShell管理单元XYZ,因为它已添加了alerady.验证管理单元的名称,然后重试.

在调用Add-PSSnapin之前,如何检查每个脚本以查看是否已加载管理单元?

powershell

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