标签: powershell

在foreach中运行程序

我试图让这个简单的Powershell脚本工作,但我觉得有些根本错误;-)

ls | foreach { "C:\Working\tools\custom-tool.exe" $_ }
Run Code Online (Sandbox Code Playgroud)

我基本上想要在目录中获取文件并将它们作为参数逐个传递给自定义工具.

powershell

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

一个更好的Linux shell?

我使用bash,已经这样做了十多年 - 但偶尔我想知道Linux shell世界是否有任何重大的新发展.

几年前,微软发布了PowerShell,这看起来非常有趣.Linux shell中是否有类似的创新?

linux bash shell powershell

27
推荐指数
5
解决办法
4773
查看次数

在Powershell中将内容插入文本文件

我想在Powershell中将内容添加到文本文件的中间.我正在搜索特定模式,然后在其后添加内容.请注意,这是在文件的中间.

我现在拥有的是:

 (Get-Content ( $fileName )) | 
      Foreach-Object { 
           if($_ -match "pattern")
           {
                #Add Lines after the selected pattern
                $_ += "`nText To Add"
           }
      }
  } | Set-Content( $fileName )
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用.我假设因为$ _是不可变的,或者因为+ =运算符没有正确修改它?

将文本附加到$ _的方法是什么,这将反映在以下Set-Content调用中?

powershell

27
推荐指数
2
解决办法
6万
查看次数

将错误和输出写入文本文件和控制台

我试图将执行脚本的整个输出(包括错误)同时写入控制台和文件.我尝试了几种不同的选择:

.\MyScript.ps1 | tee -filePath C:\results.txt # only the output to the file
.\MyScript.ps1 2> C:\results.txt # only the errors to the file and not the console
.\MyScript.ps1 > C:\results.txt # only the output to the file and not the console 
Run Code Online (Sandbox Code Playgroud)

我希望我可以使用该文件来查看输出/错误.

编辑:

这是我目前的测试脚本.期望的结果是可以看到所有三个消息.

function Test-Error 
{
    echo "echo"
    Write-Warning "warning"
    Write-Error "error"       
}

Test-Error 2>&1 | tee -filePath c:\results.txt
Run Code Online (Sandbox Code Playgroud)

powershell redirect

27
推荐指数
3
解决办法
8万
查看次数

PowerShell中的线程如何工作?

我想在PowerShell中将一些文件解析操作与网络活动并行化.快速google for it,start-thread看起来像一个解决方案,但是:

术语"start-thread"不被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.

当我尝试开始工作时,同样的事情发生了.

我也试过摆弄System.Threading.Thread

[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
#This next errors, something about the arguments I can't figure out from the documentation of .NET
$tstart = new-object System.Threading.ThreadStart({DoSomething}) 
$thread = new-object System.Threading.Thread($tstart) 
$thread.Start()
Run Code Online (Sandbox Code Playgroud)

所以,我认为最好的方法是在使用start-thread时知道我做错了什么,因为它似乎适用于其他人.我使用v2.0,我不需要向下兼容.

powershell multithreading

27
推荐指数
2
解决办法
4万
查看次数

如何使用Powershell Web Admin Commandlets更改IIS站点的应用程序池

以下代码演示了我认为可能有用的内容,但它不会更改应用程序池 - 它仍保持设置为当前值(即使$ site对象确实更新):

import-module WebAdministration

$site = get-item "IIS:\Sites\Project"
$site.ApplicationPool = "ProjectAppPool"
$site | set-item
Run Code Online (Sandbox Code Playgroud)

如果使用New-WebSite创建指定-ApplicationPool参数的站点,则会按预期创建.我必须使用哪些Powershell IIS web命令将现有站点的应用程序池更改为不同的东西?

iis powershell

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

通过PowerShell禁用Windows Server上的IE安全性

它一直在发生,我用Windows服务器启动了一个虚拟机,由于IE的安全性,我无法访问互联网.有没有人有一个简单的PowerShell脚本来禁用IE安全性?

powershell internet-explorer windows-server-2008-r2

27
推荐指数
2
解决办法
3万
查看次数

查找和替换文件失败

我试图使用以下方法在文件中查找和替换.

Function Find-Replace ($FileFullpath, $FindString, $ReplacementString) {

   Get-Content $FileFullpath | 
   Foreach-Object {$_ -replace $FindString, $ReplacementString } |  
   Set-Content $FileFullpath

}

Find-Replace "c:\program files (x86)\MyProj\web.config" $OldServiceName $NewServiceName
Run Code Online (Sandbox Code Playgroud)

但我总是得到错误.

Set-Content:进程无法访问文件'c:\ program files(x86)\ MyProj\web.config',因为它正由另一个进程使用.

该文件不会在任何地方打开.我认为Get-content尚未发布该文件.

为什么会这样?如何在同一个文件中查找和替换没有问题?

powershell powershell-2.0

27
推荐指数
1
解决办法
9984
查看次数

如何并行多次执行PowerShell函数?

我不知道需要多线程,工作为主,或异步,但基本上我有一个PowerShell脚本函数,它的几个参数是否调用这个,我需要使用不同的参数调用了几次,有这些运行在平行下.

目前,我调用这样的函数:

Execute "param1" "param2" "param3" "param4"
Run Code Online (Sandbox Code Playgroud)

如何在不等待每次调用Execute返回调用者的情况下多次调用它?

目前我正在运行v2.0,但如有必要,我可以更新

编辑:这是我到目前为止,这是行不通的:

$cmd = {
    param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
    Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
}

Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

无法将'system.object []'转换为参数'initializationscript'所需的'system.management.automation.scriptblock'类型.不支持指定的方法

编辑2:我修改了我的脚本,但我仍然得到上面提到的错误.这是我的mod:

$cmd = {
    param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
    Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
}

Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName
Run Code Online (Sandbox Code Playgroud)

powershell asynchronous

27
推荐指数
3
解决办法
6万
查看次数

尝试从npm安装Angular 2 Material - 源文本中无法识别的标记

我从PowerShell尝试以下命令(作为管理员)

npm install @angular2-material
Run Code Online (Sandbox Code Playgroud)

并得到错误:

Unrecognized token in source text.
At line:1 char:13
+ npm install  <<<< @angular2-material
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnrecognizedToken
Run Code Online (Sandbox Code Playgroud)

我甚至尝试过只获取一个A2M组件(核心)并得到相同的错误:

npm install @angular2-material/{core}
Run Code Online (Sandbox Code Playgroud)

知道我做错了什么吗?
npm:3.9.5
节点:6.2.2
操作系统:Win7

更新:
我在A2M GitHub上发现了以下问题 - 看起来它可能是PS的一个问题.
https://github.com/angular/material2/issues/297

更新2:
使用cmd代替PS工作.

powershell node.js npm angular

27
推荐指数
2
解决办法
9418
查看次数