标签: powershell

PowerShell - Start-Process和Cmdline开关

我可以运行这个罚款:

$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" 
start-process $msbuild -wait
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此代码(下面)时,我收到一个错误:

$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo" 
start-process $msbuild -wait
Run Code Online (Sandbox Code Playgroud)

有没有办法可以使用start-process将参数传递给MSBuild?我愿意不使用启动过程,我使用它的唯一原因是我需要将"命令"作为变量.

当我 在一行上有
C:\ WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe/v:q/nologo
时,如何在Powershell中处理它?

我应该使用某种eval()类型的函数吗?

powershell command-line start-process

72
推荐指数
4
解决办法
21万
查看次数

PowerShell数组初始化

在PowerShell中初始化数组的最佳方法是什么?

例如,代码

$array = @()
for($i=0; $i -lt 5;$i++)
{
    $array[$i] = $FALSE
}
Run Code Online (Sandbox Code Playgroud)

生成错误

Array assignment failed because index '0' was out of range.
At H:\Software\PowerShell\TestArray.ps1:4 char:10
+         $array[$ <<<< i] = $FALSE
Run Code Online (Sandbox Code Playgroud)

arrays powershell

71
推荐指数
9
解决办法
19万
查看次数

如何使用Invoke-Command传递命名参数?

我有一个脚本,我可以通过Invoke-Command远程运行

Invoke-Command -ComputerName (Get-Content C:\Scripts\Servers.txt) `
               -FilePath C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1
Run Code Online (Sandbox Code Playgroud)

只要我使用默认参数,它就可以正常工作.但是,该脚本有2个名为[switch]的参数(-Debug和-Clear)

如何通过Invoke-Command传递切换参数?我已经尝试了-ArgumentList但是我遇到了错误所以我必须要有语法错误或者其他什么.任何帮助是极大的赞赏.

powershell invoke-command

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

PowerShell中使用的GetType,变量之间的差异

什么是变量之间的差异$a和$b?

$a = (Get-Date).DayOfWeek
$b = Get-Date | Select-Object DayOfWeek
Run Code Online (Sandbox Code Playgroud)

我试着检查一下

$a.GetType
$b.GetType

MemberType          : Method
OverloadDefinitions : {type GetType()}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : type GetType()
Name                : GetType
IsInstance          : True

MemberType          : Method
OverloadDefinitions : {type GetType()}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : type GetType()
Name                : GetType
IsInstance          : True
Run Code Online (Sandbox Code Playgroud)

但是,尽管这些变量的输出看起来不同,但似乎没有区别.

powershell powershell-2.0

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

为什么我的PowerShell退出代码始终为"0"?

我有一个PowerShell脚本如下

##teamcity[progressMessage 'Beginning build']
# If the build computer is not running the appropriate version of .NET, then the build will not run. Throw an error immediately.
if( (ls "$env:windir\Microsoft.NET\Framework\v4.0*") -eq $null ) {
    throw "This project requires .NET 4.0 to compile. Unfortunately .NET 4.0 doesn't appear to be installed on this machine."
    ##teamcity[buildStatus status='FAILURE' ]
}

##teamcity[progressMessage 'Setting up variables']
# Set up variables for the build script
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$v4_net_version = (ls …
Run Code Online (Sandbox Code Playgroud)

powershell teamcity exit-code

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

如何在PowerShell中打破线条?

我[ 对PowerShell来说是全新的 ] 并且在循环中连接一个字符串,如果出现特殊情况我应该插入换行符...我该怎么做?

基本上寻找相当于\n.

$str = ""
foreach($line in $file){
  if($line -Match $review){ #Special condition
    $str += ANSWER #looking for ANSWER
  }
  #code.....
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我已经尝试过

"\n" '\n' "\N" '\N' "\r" '\r' "\R" '\R' '`n' '`r' '-n' '-r' 
Run Code Online (Sandbox Code Playgroud)

powershell line-breaks

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

70
推荐指数
5
解决办法
6万
查看次数

将PowerShell的默认输出编码更改为UTF-8

默认情况下,当您将命令的输出重定向到文件或将其传递到PowerShell中的其他内容时,编码为UTF-16,这是无用的.我想把它改成UTF-8.

它可以通过替换>foo.txt语法来逐个进行,| out-file foo.txt -encoding utf8但是每次都必须重复这是很尴尬的.

在PowerShell中设置内容的持久方法是将它们放入\Users\me\Documents\WindowsPowerShell\profile.ps1; 我已经验证这个文件确实是在启动时执行的.

有人说输出编码可以设置,$PSDefaultParameterValues = @{'Out-File:Encoding' = 'utf8'}但我已经尝试过,它没有任何效果.

https://blogs.msdn.microsoft.com/powershell/2006/12/11/outputencoding-to-the-rescue/谈到$OutputEncoding乍一看似乎应该是相关的,但后来谈到输出被编码在ASCII中,这不是实际发生的事情.

如何设置PowerShell使用UTF-8?

powershell utf-8 character-encoding

70
推荐指数
1
解决办法
8万
查看次数

如何使用PowerShell执行.sql文件?

我有一个 .sql文件.我试图通过Powershell脚本传递连接字符串详细信息并调用.sql文件.

我正在搜索并想出了一个与之相关的cmdlet Invoke-sqlcmd.当我试图找到一个与SQL相对应的模块时,我在我的机器中找不到任何一个模块.

我应该在我的机器中安装任何东西(机器已经有SQL Server Management Studio 2008 R2)来获取模块,还是有简单的方法.sql使用Powershell 执行文件?

sql sql-server powershell powershell-module

69
推荐指数
5
解决办法
11万
查看次数

如果文件夹不存在,则创建该文件夹 - "项目已存在"

我试图使用PowerShell创建一个文件夹,如果它不存在所以我做了:

$DOCDIR = [Environment]::GetFolderPath("MyDocuments")
$TARGETDIR = "$DOCDIR\MatchedLog"
if(!(Test-Path -Path MatchedLog )){
   New-Item -ItemType directory -Path $DOCDIR\MatchedLog
}
Run Code Online (Sandbox Code Playgroud)

这给了我文件夹已经存在的错误,但它不应该尝试创建它.

我不确定这里有什么问题

New-Item:具有指定名称C:\ Users\l\Documents\MatchedLog的项目已存在.在C:\ Users\l\Documents\Powershell\email.ps1:4 char:13 + New-Item <<<< -ItemType directory -Path $ DOCDIR\MatchedLog + CategoryInfo:ResourceExists:(C:\ Users\l. ... ents\MatchedLog:String)[New-Item],IOException + FullyQualifiedErrorId:DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand`

powershell

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