我正在PowerShell中编写一个工具,其中包括一个基于用户输入创建文件夹结构的功能。以下是相关代码:
<img class="pic" src="https://i.stack.imgur.com/FHNjC.png" />
<div class="invisible"></div>
.invisible {
position:relative;
top:-350px;
left: 100px;
background:#000;
width:200px;
height:300px;
}
如果我运行它,它会触发catch语句并抛出以下错误:
function set-drvsource{
Write-Host "INFO: Querying Driver Source" -ForegroundColor Yellow
}
try{
Write-Host "INFO: Creating standard folder structure" -ForegroundColor Yellow
New-Item -ItemType Directory -Path "$drvpath" | Out-Null
New-Item -ItemType Directory -Path "$drvpath\Source" | Out-Null
New-Item -ItemType Directory -Path "$drvpath\Package" | Out-Null
}
catch{
Write-Host "ERROR: Could not create folder structure" -ForegroundColor Red
Exit
}
Write-Host "INFO: Moving local source to new source" -ForegroundColor Yellow
Copy-Item $src "$drvpath\Source" -Recurse
}
function main{
set-drvsource
}
$drvroot = "\\server\share\tmp"
$src = Read-Host "Please provide local driver source path"
$vendor = Read-Host "Device Vendor eg. DELL, HP, Microsoft"
$dmd = Read-Host "Device Model eg. HP Elitebook 840 G3"
$dos = Read-Host "Operating System eg. W7 or W10"
$dac = Read-Host "x64 or x86"
$drvpath = "$drvroot\$vendor\$dmd $dos $dac"
有趣的是,如果我在新的PowerShell会话中运行以下内容,它会按预期工作:
$Error[0]
New-Item : Object reference not set to an instance of an object.
At line:9 char:1
+ New-Item -ItemType Directory -Path "$drvpath" | Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-Item], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.NewI
temCommand
我加倍检查并且$ drvpath确实存活到函数中,所以它不是范围问题? 任何帮助将非常感激! :'(
编辑:输出$ ERROR [0] | fl -force
$drvroot = "\\server\share\tmp"
$vendor = Read-Host "Vendor"
$dmd = Read-Host "Device Model eg. HP Elitebook 840 G3"
$dos = Read-Host "Operating System eg. W7 or W10"
$dac = Read-Host "x64 or x86"
$drvpath = "$drvroot\$vendor\$dmd $dos $dac"
New-Item -ItemType Directory -Path "$drvpath" | Out-Null
New-Item -ItemType Directory -Path "$drvpath\Source" | Out-Null
New-Item -ItemType Directory -Path "$drvpath\Package" | Out-Null
答案 0 :(得分:0)
使用FileSystem提供程序定义$ drvroot变量,就像PetSerAl建议的那样,解决了这个问题。