2017-02-13 7 views
0

Ich schreibe ein Tool in Powershell, die eine Funktion zum Erstellen einer Ordnerstruktur basierend auf Benutzereingaben enthält. Unten ist der entsprechende Code:PowerShell New-Objekt - Objektreferenz nicht auf eine Instanz eines Objekts gesetzt

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" 

Wenn ich dies ausführen, löst er die Anweisung fangen und führt den folgenden Fehler:

$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 

Das Interessante daran ist, wenn ich das folgende in einer neuen Powershell Sitzung, es funktioniert wie erwartet:

$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 

ich habe verdoppelt geprüft und $ drvpath an die Funktion überlebt durch, so dass sie nicht ein Umfang Problem? Jede Hilfe wäre sehr willkommen! : '(

EDIT:. Ausgabe von $ 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

anzeigen Ausgang von '$ Error [0] | fl -Force'. – PetSerAl

+0

Hinzugefügt @PetSerAl – FishFenly

+0

' \\ Server \ Freigabe \ tmp' -> 'Dateisystem :: \\ Server \ Freigabe \ tmp' – PetSerAl

Antwort

0

die $ drvroot Variable mit dem Filesystem-Anbieter definieren, wie PetSerAl vorgeschlagen, löste das Problem

Verwandte Themen