2017-02-13 61 views
0

我在PowerShell中編寫了一個工具,其中包括一個基於用戶輸入創建文件夾結構的功能。下面是相關代碼: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的輸出[0] | 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  : 
+0

顯示輸出從'$ Error [0] | fl -Force'。 – PetSerAl

+0

加入@PetSerAl – FishFenly

+0

'\\ server \ share \ tmp' - >'filesystem :: \\ server \ share \ tmp' – PetSerAl

回答

0

定義$ drvroot變量與FileSystem提供,如PetSerAl建議,解決問題

相關問題