如何使用PowerShell(版本1.0)脚本删除文件上的ReadOnly属性?
我写了一个简单的批处理文件作为PowerShell脚本,我在运行时遇到错误.
它位于我路径的脚本目录中.
__PRE__
我查看了帮助,但这并没有帮助.
我正在编写一个用于自定义配置文件的脚本.我想在这个文件中替换多个字符串实例,我尝试使用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) Start-Process访问StandardError和StandardOutput属性时,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) 我需要这些命令来检查Windows上的日志文件,但我没有安装任何程序,我喜欢Powershell与Windows.
我必须从批处理文件中调用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脚本打开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)
当我尝试打开连接时,它说:
登录失败
似乎%操作在管道之后启动脚本块,尽管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) 我需要在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脚本并确保空格没有问题?
我有一组PowerShell脚本有时可以一起运行,有时一次运行.每个脚本都要求加载某个管理单元.
现在每个脚本都Add-PSSnapin XYZ在开头调用.
现在,如果我背靠背地运行多个脚本,则后续脚本会抛出:
无法添加Windows PowerShell管理单元XYZ,因为它已添加了alerady.验证管理单元的名称,然后重试.
在调用Add-PSSnapin之前,如何检查每个脚本以查看是否已加载管理单元?
powershell ×10
scripting ×3
arguments ×1
c# ×1
command-line ×1
escaping ×1
replace ×1
syntax ×1
windows ×1