你如何在PowerShell(1.0或2.0)中注释掉代码?
每当我需要引用一个公共模块或脚本时,我喜欢使用相对于当前脚本文件的路径,这样,我的脚本总能找到库中的其他脚本.
那么,确定当前脚本目录的最佳标准方法是什么?目前,我正在做:
$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
Run Code Online (Sandbox Code Playgroud)
我知道在模块(.psm1)中你可以使用它$PSScriptRoot
来获取这些信息,但这不会在常规脚本(即.ps1文件)中设置.
获取当前PowerShell脚本文件位置的规范方法是什么?
我正在使用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) 我在所有文件夹中搜索文件.
Copyforbuild.bat
很多地方都有,我想递归搜索.
$File = "V:\Myfolder\**\*.CopyForbuild.bat"
Run Code Online (Sandbox Code Playgroud)
我怎么能在PowerShell中做到这一点?
我想只删除特定文件夹中超过15天前创建的文件.我怎么能用PowerShell做到这一点?
突然之间,升级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
如何将数组对象转换为字符串?
我试过了:
$a = "This", "Is", "a", "cat"
[system.String]::Join(" ", $a)
Run Code Online (Sandbox Code Playgroud)
没有运气.PowerShell有哪些不同的可能性?
我想计算一些内容的MD5校验和.我如何在PowerShell中执行此操作?
在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.有区别吗?
我需要获取所有文件,包括属于特定类型的子文件夹中存在的文件.
我正在做这样的事情,使用Get-ChildItem:
Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
Run Code Online (Sandbox Code Playgroud)
但是,它只返回文件名而不是整个路径.