我正在用powershell编写一个工具,其中包含一个根据用户输入创建文件夹结构的函数。相关代码如下:
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"如果我运行它,它会触发catch语句并抛出以下错误:
$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有趣的是,如果我在新的PowerShell会话中运行以下命令,它会按预期工作:
$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我已经检查了两次,$drvpath确实通过了函数,所以这不是一个作用域问题?任何帮助都将不胜感激!:'(
编辑:$ERROR | fl -force的输出
Exception : System.NullReferenceException: Object reference not
set to an instance of an object.
at Microsoft.ConfigurationManagement.PowerShell.Prov
ider.CMDriveProvider.NewItem(String path, String
itemTypeName, Object newItemValue)
at System.Management.Automation.SessionStateInternal
.NewItemPrivate(CmdletProvider providerInstance,
String path, String type, Object content,
CmdletProviderContext context)
TargetObject :
CategoryInfo : NotSpecified: (:) [New-Item], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Comm
ands.NewItemCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at set-drvsource, C:\temp\quick
import_cli-1.1\quickimport_cli-1.1\Invoke-QuickImport.p
s1: line 51
at main, C:\temp\quickimport_cl
i-1.1\quickimport_cli-1.1\Invoke-QuickImport.ps1: line
101
at <ScriptBlock>, C:\temp\quick
import_cli-1.1\quickimport_cli-1.1\Invoke-QuickImport.p
s1: line 165
PipelineIterationInfo : {}
PSMessageDetails : 发布于 2017-02-15 20:21:56
按照PetSerAl的建议,使用FileSystem提供程序定义$drvroot变量解决了问题。
https://stackoverflow.com/questions/42206283
复制相似问题