标签: powershell

使用PowerShell查找与模式匹配的第一个文件

我想在目录中选择任何一个".xls"文件.问题是dir命令可以返回不同的类型.

gci *.xls
Run Code Online (Sandbox Code Playgroud)

将返回

  • object []如果有多个文件
  • FileInfo如果只有一个文件
  • 如果没有文件,则返回null

我可以处理null,但我如何选择"第一个"文件?

powershell

49
推荐指数
1
解决办法
4万
查看次数

从powershell中的列中选择不同的项目

如果我在PowerShell中发出以下命令,我会收到很多行.

PS C:\Users\benh> get-command

CommandType     Name                               ModuleName                         Definition
-----------     ----                               ----------                         ----------
Cmdlet          Get-Variable                       Microsoft.PowerShell.Utility       Get-Variable...
Cmdlet          Get-WebAppDomain                   WebAdministration                  Get-WebAppDomain...
Cmdlet          Get-WebApplication                 WebAdministration                  Get-WebApplication...
Cmdlet          Get-WebAppPoolState                WebAdministration                  Get-WebAppPoolState...
...
Cmdlet          Get-WinEvent                       Microsoft.PowerShell.Diagnostics   Get-WinEvent...
Cmdlet          Get-WmiObject                      Microsoft.PowerShell.Management    Get-WmiObject...
Cmdlet          Get-WSManCredSSP                   Microsoft.WSMan.Management         Get-WSManCredSSP...
Cmdlet          Get-WSManInstance                  Microsoft.WSMan.Management         Get-WSManInstance...
Cmdlet          Group-Object                       Microsoft.PowerShell.Utility       Group-Object...
Cmdlet          Import-Alias                       Microsoft.PowerShell.Utility       Import-Alias...
Cmdlet          Import-Clixml                      Microsoft.PowerShell.Utility       Import-Clixml...
Cmdlet          Import-Counter                     Microsoft.PowerShell.Diagnostics   Import-Counter...
Cmdlet          Import-Csv                         Microsoft.PowerShell.Utility       Import-Csv...
Cmdlet          Import-LocalizedData               Microsoft.PowerShell.Utility       Import-LocalizedData...
Cmdlet          Import-Module                      Microsoft.PowerShell.Core          ...
Run Code Online (Sandbox Code Playgroud)

我想要做的是获取Get-Command返回的所有不同的ModuleNames.如何使用PowerShell执行此操作?

在伪C#中:

PowerShell.Exec("Get-Command").Select(a=> a.ModuleName).Distinct();
Run Code Online (Sandbox Code Playgroud)

提前致谢!

powershell distinct-values

49
推荐指数
4
解决办法
6万
查看次数

如何在软件包管理器控制台中"更新 - 打包"到以前的版本?

我在我的项目中安装了一个软件包版本,但在测试期间我发现了它的问题.我尝试了显而易见的事情,Update-Package -Id Foo.Bar -Version 1.0.0 -Force但Update-Package cmdlet没有-Force参数,并且它不允许更新早期版本.如何降级我的包依赖项(不使用源代码控制!)


注意:此问题现在无关紧要,因为Update-Package MyPackage -Version [an earlier version]在最新版本的NuGet软件包管理器中开箱即用.你甚至不需要-Force开关.

console powershell nuget nuget-package

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

在Powershell中,将字符串转换为int的惯用方法是什么?

我发现的唯一方法是直接投射:

> $numberAsString = "10"
> [int]$numberAsString
10
Run Code Online (Sandbox Code Playgroud)

这是Powershell的标准方法吗?是否期望在之前进行测试以确保转换成功,如果是,如何?

powershell

49
推荐指数
5
解决办法
10万
查看次数

我如何从Powershell中的catch块中重新抛出异常?

我应该以与.Net相同的方式处理异常处理吗?

那么,如何在Powershell中从catch块中重新抛出异常?

'throw'是enoungh还是'throw $ _'会更好?

powershell exception-handling exception

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

PowerShell:按字段值检索JSON对象

考虑这种格式的JSON:

"Stuffs": [
    {
        "Name": "Darts",
        "Type": "Fun Stuff"
    },
    {
        "Name": "Clean Toilet",
        "Type": "Boring Stuff"
    }
]
Run Code Online (Sandbox Code Playgroud)

在PowerShell 3中,我们可以获得一个Stuffs列表:

$JSON = Get-Content $jsonConfigFile | Out-String | ConvertFrom-Json
Run Code Online (Sandbox Code Playgroud)

假设我们不知道列表的确切内容,包括对象的排序,我们如何检索具有Name字段的特定值的对象?

蛮力,我们可以遍历列表:

foreach( $Stuff in $JSON.Stuffs ) { 
Run Code Online (Sandbox Code Playgroud)

但我希望存在一种更直接的机制(类似于C#中的Lync或Lambda表达式).

powershell json powershell-3.0

49
推荐指数
4
解决办法
11万
查看次数

PowerShell内联If(IIf)

如何在PowerShell中使用内联If(IIf,另请参阅:http://en.wikipedia.org/wiki/IIf或ternary If)创建一个语句?

如果您还认为这应该是本机PowerShell功能,请将其投票:https://connect.microsoft.com/PowerShell/feedback/details/1497806/iif-statement-if-shorthand

powershell iif

49
推荐指数
4
解决办法
6万
查看次数

如何在powershell中将字符串转换为整数

我有一个带数字的目录列表.我必须找到最高的数字并将其递增1并使用该增量值创建一个新目录.我能够对下面的数组进行排序,但我无法增加最后一个元素,因为它是一个字符串.

如何将此数组元素转换为整数?

PS C:\Users\Suman\Desktop> $FileList

Name
----
11
2
1
Run Code Online (Sandbox Code Playgroud)

powershell

49
推荐指数
1
解决办法
15万
查看次数

在powershell中保存XML文件需要完整的路径.为什么?

我正在使用powershell的XML功能来修改.config文件.除非我提供完整的路径名,否则调用XMLDocument.Save没有任何效果.

# Open the xml file
$config = [xml](get-content web.config)
#
# modify the XML
$config.SelectNodes("./configuration/connectionStrings/add[@name='LocalSqlServer']") | % { $connNode = $_ }
$connNode.connectionString = $connNode.connectionString -replace '^(.*)Server=[^;]+(.*)$', '$1Server=192.168.5.2$2'
#
#
# Now I save it again
#
# This doesn't save it!
$config.Save("web.config");
# However, this works
$config.Save("{0}\\web.config" -f (get-location));
Run Code Online (Sandbox Code Playgroud)

为什么$ config.Save("web.config")不起作用?

除了本地目录以外,我最终还是将它保存在其他地方吗?

xml powershell

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

从函数中设置全局PowerShell变量,其中全局变量名是传递给函数的变量

我需要从函数设置一个全局变量,我不太清楚如何做到这一点.

# Set variables
$global:var1
$global:var2
$global:var3

function foo ($a, $b, $c)
{
    # Add $a and $b and set the requested global variable to equal to it
    $c = $a + $b
}
Run Code Online (Sandbox Code Playgroud)

调用函数:

foo 1 2 $global:var3
Run Code Online (Sandbox Code Playgroud)

最终结果:

$ global:var3设置为3

或者如果我这样调用函数:

foo 1 2 $global:var2
Run Code Online (Sandbox Code Playgroud)

最终结果:

$ global:var2设置为3

我希望这个例子有意义.传递给函数的第三个变量是要设置的变量的名称.

powershell

48
推荐指数
4
解决办法
28万
查看次数