标签: powershell

如何通过PowerShell中的属性名称获取对象的属性值?

在PowerShell中,如何通过指定对象的名称(字符串)来获取对象的属性值?我想要以下内容:

$obj = get-something

# View the object's members:
$obj | gm

# I could retrieve a property by doing so:
write-host $obj.SomeProp

# But for many purposes, I would really want to:
write-host $obj | Get-PropertyByName "SomeProp"
Run Code Online (Sandbox Code Playgroud)

在PowerShell中有类似"Get-PropertyByName"的东西吗?

powershell

108
推荐指数
5
解决办法
25万
查看次数

如何将PowerShell版本从2.0升级到3.0

我使用的操作系统是Windows 7,此处安装的PowerShell版本是2.0.我有可能将其升级到3.0或4.0版本吗?

因为版本2.0无法识别cmdlet.

powershell upgrade powershell-2.0 powershell-3.0

107
推荐指数
5
解决办法
31万
查看次数

如何创建永久PowerShell别名

我想创建一个aliascmdlet不到期后,我关闭的PowerShell当前会话,让我们说我有这个别名:

C:\Users\Aymen> New-Alias Goto Set-Location
Run Code Online (Sandbox Code Playgroud)

这完美地创建了Goto别名,但我想在关闭当前会话后使用它,我该如何实现.

注意:

PowerShell帮助系统建议我可以导出我创建的别名,并在下次打开新会话时导入它们,实际上这并不是我真正想要的,因为有没有直接清晰的方法来保持别名后我通过不同的会话创建它

powershell

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

PowerShell支持常量吗?

我想在PowerShell中声明一些整数常量.

有没有好办法呢?

powershell constants

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

Powershell.exe的路径(v 2.0)

Powershell(2.0版)在哪里?Powershell.exe的路径是什么?我安装了Windows Server 2008和Powershell.当我查看此文件夹时:

PS C:\Windows\System32\WindowsPowerShell> dir


    Directory: C:\Windows\System32\WindowsPowerShell


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         20.4.2010     17:09            v1.0
Run Code Online (Sandbox Code Playgroud)

我只有Powershell v1.0.但是当我打字的时候

PS C:\> $Host.version

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1


PS C:\>
Run Code Online (Sandbox Code Playgroud)

它表明我安装了v2.0.

powershell path

106
推荐指数
5
解决办法
29万
查看次数

如何从PowerShell命令行查找Windows版本?

如何找到我正在使用的Windows版本?

我正在使用PowerShell 2.0并尝试:

PS C:\> ver
The term 'ver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify tha
t the path is correct and try again.
At line:1 char:4
+ ver <<<< 
    + CategoryInfo          : ObjectNotFound: (ver:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

windows powershell powershell-2.0

106
推荐指数
13
解决办法
33万
查看次数

你如何使用PowerShell 2.0进行"暂停"?

好的,我正在失去它.PowerShell很讨厌我.我想要一个暂停对话框出现,但它不会.

PS W:\>>> $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."
At line:1 char:23
+ $host.UI.RawUI.ReadKey <<<< ("NoEcho")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Run Code Online (Sandbox Code Playgroud)

powershell

105
推荐指数
5
解决办法
19万
查看次数

当一个调用只返回一个对象时,如何强制Powershell返回一个数组呢?

我正在使用Powershell在Web服务器上设置IIS绑定,并且遇到以下代码的问题:

$serverIps = gwmi Win32_NetworkAdapterConfiguration 
    | Where { $_.IPAddress } 
    | Select -Expand IPAddress 
    | Where { $_ -like '*.*.*.*' } 
    | Sort

if ($serverIps.length -le 1) {
    Write-Host "You need at least 2 IP addresses for this to work!"
    exit
}

$primaryIp = $serverIps[0]
$secondaryIp = $serverIps[1]
Run Code Online (Sandbox Code Playgroud)

如果服务器上有2个以上的IP,那么很好 - Powershell返回一个数组,我可以查询数组长度并提取第一个和第二个地址就好了.

问题是 - 如果只有一个IP,Powershell不返回单元素数组,它返回IP地址(作为字符串,如"192.168.0.100") - 字符串有一个.length属性,它大于1,所以测试通过,我最后得到字符串中的前两个字符,而不是集合中的前两个IP地址.

如何强制Powershell返回单元素集合,或者确定返回的"thing"是对象而不是集合?

powershell

105
推荐指数
4
解决办法
5万
查看次数

PowerShell:将整个文本文件内容存储在变量中

我想使用PowerShell将变量中的文本文件的全部内容(包括可能存在或可能不存在的尾随空行)存储起来.我还想知道文本文件中的总行数.最有效的方法是什么?

powershell

104
推荐指数
4
解决办法
15万
查看次数

powershell - 提取文件名和扩展名

我需要从例如my.file.xlsx中提取文件名和扩展名.我不知道文件或扩展名的名称,名称中可能有更多的点,所以我需要从右边搜索字符串,当我找到第一个点(或从左边开始)时,提取部分右侧和该点左侧的部分.

也许有更好的解决方案,但我在这里或其他地方找不到任何东西.谢谢

.net powershell powershell-2.0

103
推荐指数
7
解决办法
19万
查看次数