标签: powershell-2.0

913
推荐指数
8
解决办法
79万
查看次数

确定当前PowerShell脚本位置的最佳方法是什么?

每当我需要引用一个公共模块或脚本时,我喜欢使用相对于当前脚本文件的路径,这样,我的脚本总能找到库中的其他脚本.

那么,确定当前脚本目录的最佳标准方法是什么?目前,我正在做:

$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
Run Code Online (Sandbox Code Playgroud)

我知道在模块(.psm1)中你可以使用它$PSScriptRoot来获取这些信息,但这不会在常规脚本(即.ps1文件)中设置.

获取当前PowerShell脚本文件位置的规范方法是什么?

powershell powershell-2.0

492
推荐指数
11
解决办法
46万
查看次数

如何使用Get-ChildItem仅获取目录?

我正在使用PowerShell 2.0,我想要管理某个路径的所有子目录.以下命令输出所有文件和目录,但我无法弄清楚如何过滤掉文件.

Get-ChildItem c:\mypath -Recurse
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用$_.Attributes获取属性,但后来我不知道如何构建一个文字实例System.IO.FileAttributes来比较它.在cmd.exe它会

dir /b /ad /s
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0

230
推荐指数
8
解决办法
43万
查看次数

使用PowerShell进行递归文件搜索

我在所有文件夹中搜索文件.

Copyforbuild.bat 很多地方都有,我想递归搜索.

$File = "V:\Myfolder\**\*.CopyForbuild.bat"
Run Code Online (Sandbox Code Playgroud)

我怎么能在PowerShell中做到这一点?

powershell powershell-2.0

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

使用PowerShell删除超过15天的文件

我想只删除特定文件夹中超过15天前创建的文件.我怎么能用PowerShell做到这一点?

powershell powershell-2.0 powershell-3.0

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

由于"无法初始化PowerShell主机",无法安装nuget包

突然之间,升级Nuget软件包时出现此错误.我遇到过的修复工作都没有.我正在使用Visual Studio 2013.

已经安装了"Newtonsoft.Json 6.0.3".

将"Newtonsoft.Json 6.0.3"添加到Tournaments.Notifications中.

成功将"Newtonsoft.Json 6.0.3"添加到Tournaments.Notifications中.

执行脚本文件'F:\ My Webs\BasketballTournaments\MainBranch\packages\Newtonsoft.Json.6.0.3\tools\install.ps1'.

无法初始化PowerShell主机.如果PowerShell执行策略设置设置为AllSigned,请打开程序包管理器控制台以首先初始化主机.

包管理器控制台

尝试在"FileSystem"提供程序上执行InitializeDefaultDrives操作失败.

如果我等待初始化在控制台中完成,我可以添加一些包.

powershell powershell-2.0 nuget nuget-package visual-studio-2013

171
推荐指数
11
解决办法
10万
查看次数

如何在PowerShell中将数组对象转换为字符串?

如何将数组对象转换为字符串?

我试过了:

$a = "This", "Is", "a", "cat"
[system.String]::Join(" ", $a)
Run Code Online (Sandbox Code Playgroud)

没有运气.PowerShell有哪些不同的可能性?

powershell powershell-2.0

159
推荐指数
4
解决办法
43万
查看次数

如何在PowerShell中获取MD5校验和

我想计算一些内容的MD5校验和.我如何在PowerShell中执行此操作?

powershell powershell-2.0

157
推荐指数
10
解决办法
23万
查看次数

我什么时候应该使用Write-Error vs. Throw?终止与非终止错误

在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.有区别吗?

error-handling powershell powershell-2.0

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

获取PowerShell中文件的完整路径

我需要获取所有文件,包括属于特定类型的子文件夹中存在的文件.

我正在做这样的事情,使用Get-ChildItem:

Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
Run Code Online (Sandbox Code Playgroud)

但是,它只返回文件名而不是整个路径.

powershell powershell-2.0

124
推荐指数
10
解决办法
34万
查看次数