如何从PowerShell中的文件.dll或.exe文件中获取版本信息?
我在特别感兴趣File Version,但其他版本信息(也就是Company,Language,Product Name,等)将是有益的为好.
我希望我的PowerShell脚本能够打印出这样的内容:
Enabling feature XYZ......Done
Run Code Online (Sandbox Code Playgroud)
脚本看起来像这样:
Write-Output "Enabling feature XYZ......."
Enable-SPFeature...
Write-Output "Done"
Run Code Online (Sandbox Code Playgroud)
但Write-Output总是在最后打印一个新行,所以我的输出不在一行.有没有办法做到这一点?
我对打印(回显)到控制台的各种方法有点困惑.我已经看到有多种方法可以将输出写入控制台,例如:
Write-Host "Hello world1"
"Hello World2"
Out-Host -InputObject "Hello World3"
Run Code Online (Sandbox Code Playgroud)
这三种方式都将打印到控制台.中间的一个更简单,更简洁,更容易使用.我还发现当你编写一个函数时:
function GetValues()
{
"1"
"2"
}
Run Code Online (Sandbox Code Playgroud)
它仍然在管道中返回两个字符串:
我仍然可以打印出值:
foreach ($s in GetValues)
{
Write-Host "s: " $s
}
Run Code Online (Sandbox Code Playgroud)
我发现的事情是,仅使用引用的字符串并不总是出现在自定义主机上,而且我必须使用Write-Host来获取要在自定义主机上打印的值.
不知怎的,我发现这令人困惑.被"Print something"认为是一个别名Write-Host或有什么意图?
我有一个PowerShell脚本,使用脚本的当前目录执行一些操作.因此,当在该目录中时,运行.\script.ps1正常.
现在我想从不同的目录调用该脚本而不更改脚本的引用目录.所以我想调用..\..\dir\script.ps1并仍然希望该脚本在其目录中调用时的行为.
我该怎么做,或者如何修改脚本以便它可以从任何目录运行?
在PoshCode上查看Get-WebFile脚本,http: //poshcode.org/3226 ,我注意到这个奇怪的对我来说:
$URL_Format_Error = [string]"..."
Write-Error $URL_Format_Error
return
Run Code Online (Sandbox Code Playgroud)
与以下相反,这是什么原因?
$URL_Format_Error = [string]"..."
Throw $URL_Format_Error
Run Code Online (Sandbox Code Playgroud)
甚至更好:
$URL_Format_Error = New-Object System.FormatException "..."
Throw $URL_Format_Error
Run Code Online (Sandbox Code Playgroud)
据我所知,你应该使用Write-Error来解决非终止错误,并使用Throw来终止错误,所以在我看来你不应该使用Write-Error然后使用Return.有区别吗?
有没有一种简单的方法可以使用PowerShell 挂钩标准的" 添加或删除程序 "功能来卸载现有的应用程序?或者检查是否安装了应用程序?
这是try catchPowerShell 2.0中的内容
$urls = "http://www.google.com", "http://none.greenjump.nl", "http://www.nu.nl"
$wc = New-Object System.Net.WebClient
foreach($url in $urls)
{
try
{
$url
$result=$wc.DownloadString($url)
}
catch [System.Net.WebException]
{
[void]$fails.Add("url webfailed $url")
}
}
Run Code Online (Sandbox Code Playgroud)
但我想要做的就是在c#中
catch( WebException ex)
{
Log(ex.ToString());
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?
正如我在PowerShell用户指南中所读到的,PowerShell的核心概念之一是命令接受并返回对象而不是文本.例如,运行会get-alias返回许多System.Management.Automation.AliasInfo对象:
PS Z:\> get-alias CommandType Name Definition ----------- ---- ---------- Alias % ForEach-Object Alias ? Where-Object Alias ac Add-Content Alias asnp Add-PSSnapIn Alias cat Get-Content Alias cd Set-Location Alias chdir Set-Location ...
现在,我如何获得这些对象的数量?
我开始研究PowerShell模型和管理单元开发.我注意到的第一件事是引用System.management.automation.dll.但是在Visual Studio中,.NET选项卡没有该程序集,也无法浏览到
C:\windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
制作基于文件的参考.
我需要获取所有文件,包括属于特定类型的子文件夹中存在的文件.
我正在做这样的事情,使用Get-ChildItem:
Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
Run Code Online (Sandbox Code Playgroud)
但是,它只返回文件名而不是整个路径.