标签: powershell-ise

如果目录不存在,请创建目录

我正在编写一个PowerShell脚本来创建几个目录(如果它们不存在).

文件系统看起来与此类似

D:\
D:\TopDirec\SubDirec\Project1\Revision1\Reports\
D:\TopDirec\SubDirec\Project2\Revision1\
D:\TopDirec\SubDirec\Project3\Revision1\
Run Code Online (Sandbox Code Playgroud)
  • 每个项目文件夹都有多个修订版.
  • 每个修订文件夹都需要Reports文件夹.
  • 某些"revisions"文件夹已包含Reports文件夹; 但是,大多数人没有.

我需要编写一个每天运行的脚本来为每个目录创建这些文件夹.

我能够编写脚本来创建文件夹,但创建几个文件夹是有问题的.

powershell powershell-ise windows-server-2008

291
推荐指数
9
解决办法
38万
查看次数

在PowerShell中重新加载路径

如果我有一个运行PowerShell ISE的实例,我安装了修改PATH的东西,或者我在PowerShell之外以任何方式修改它,那么我需要重新启动PowerShell才能看到更新的PATH变量.

有没有办法从PowerShell中重新加载路径而不重新启动它?

console powershell powershell-ise

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

在PowerShell中逐行读取文件

我想在PowerShell中逐行读取文件.具体来说,我想循环遍历文件,将每一行存储在循环中的变量中,并在该行上进行一些处理.

我知道Bash的等价物:

while read line do
    if [[ $line =~ $regex ]]; then
          # work here
    fi
done < file.txt
Run Code Online (Sandbox Code Playgroud)

关于PowerShell循环的文档不多.

powershell powershell-ise

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

如何将所需参数传递给Powershell ISE中的脚本?

见标题.

我在脚本的头部指定了所需的参数:

param ($G_ARCHIVE = $(throw "Need file to upload!"),
       $G_LOGFILE = $(throw "Need logfile!"))
Run Code Online (Sandbox Code Playgroud)

当我想用Powershell ISE调试脚本时:我该如何填写这些参数?

powershell parameter-passing powershell-ise

69
推荐指数
3
解决办法
6万
查看次数

通过命令行将变量传递给powershell脚本

我是powershell的新手,并试图自学基础知识.我需要编写一个ps脚本来解析一个文件,这并不算太难.

现在我想更改它以将变量传递给脚本.该变量将是解析字符串.现在,变量将始终为1个单词,而不是一组单词或多个单词.

这看起来很简单,但对我来说却是一个问题.这是我的简单代码:

$a = Read-Host
Write-Host $a
Run Code Online (Sandbox Code Playgroud)

当我从命令行运行脚本时,变量传递不起作用:

.\test.ps1 hello
.\test.ps1 "hello"
.\test.ps1 -a "hello"
.\test.ps1 -a hello
.\test.ps1 -File "hello"
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我已经尝试了许多没有成功的方法,脚本将值输出它.

脚本确实运行,并等待我键入一个值,当我这样做时,它会回显该值.

我只是想让它输出我传入的值,我错过了什么小事?

谢谢.

powershell powershell-ise

52
推荐指数
4
解决办法
29万
查看次数

获取当前目录中子文件夹中文件的相对路径

有没有直接的方法(通过使用cmdlet或.NET类)来获取给定路径中子文件夹中文件的相对路径?

例如,当前文件夹是C:\MyScript并没有所谓的子文件夹Data与文件Test.txt,所以我想看到Data\Test.txt的,而不是C:\MyScript\Data\Test.txt

powershell powershell-ise powershell-2.0

45
推荐指数
2
解决办法
4万
查看次数

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

PowerShell更改返回对象的类型

我正在使用PowerShell v3和Windows PowerShell ISE.我有以下功能正常工作:

function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
    # If a Namespace URI was not given, use the Xml document's default namespace.
    if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }   

    # In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
    [System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
    $xmlNsManager.AddNamespace("ns", $NamespaceURI)

    [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter

    # Try and get the …
Run Code Online (Sandbox Code Playgroud)

powershell function return-type powershell-ise powershell-3.0

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

Powershell:函数中的$ LASTEXITCODE

嗨,我注意到以下代码片段的一些奇怪的行为

function test
{
    $LASTEXITCODE = $null
    ping asdfs
    Write-Host "Last exitcode: $LASTEXITCODE"
}

test
Write-Host "Last exitcode: $LASTEXITCODE"
Run Code Online (Sandbox Code Playgroud)

这个输出是

Ping request could not find host asdfs. Please check the name and try again.
Last exitcode: 
Last exitcode: 1
Run Code Online (Sandbox Code Playgroud)

为什么$ LASTEXITCODE没有在test()函数中设置?

这是我现在遇到的一个问题的概括,当我从一个函数中调用Win32 .exe并且$ LASTEXITCODE没有返回我在函数中期望的值时

powershell powershell-ise powershell-2.0

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

PowerShell - 为整个脚本设置$ ErrorActionPreference

我今天第一次给PowerShell(v3.0)拍摄了一个镜头,并且对它的一些错误处理概念实现的奇怪方式感到非常沮丧.

我编写了以下代码(使用远程注册表PowerShell模块)

try
{
    New-RegKey -ComputerName $PCName -Key $Key -Name $Value
    Write-Host -fore Green ($Key + ": created")
}
catch
{
    Write-Host -fore Red "Unable to create RegKey: " $Key
    Write-Host -fore Red $_
}
Run Code Online (Sandbox Code Playgroud)

(这只是一个片段)

显然,PowerShell的默认行为是不捕获非终止的错误.所以我按照不同的人的建议在我的脚本顶部添加了以下行:

$ErrorActionPreference = "Stop"
Run Code Online (Sandbox Code Playgroud)

在PowerShell ISE中执行此操作确实捕获了所有错误.但是,从终端执行以下命令仍然无法捕获我的错误.

来自ISE:

PS C:\windows\system32> C:\Data\Scripts\PowerShell\Error.ps1
Errorhandling:  Stop
SOFTWARE\MySoftware does not exist. Attempting to create
Unable to create RegKey:  SOFTWARE\MySoftware
Key 'SOFTWARE\MySoftware' doesn't exist.
Run Code Online (Sandbox Code Playgroud)

从命令行:

PS C:\Data\Scripts\PowerShell> .\Error.ps1
Errorhandling:  Stop
SOFTWARE\MySoftware does not exist. Attempting to create
New-RegKey : Key …
Run Code Online (Sandbox Code Playgroud)

error-handling powershell powershell-ise powershell-3.0

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