0

我有一个脚本,我正在尝试使用 PowerShell 挂载 Windows 共享驱动器,然后我希望相同的脚本能够访问此位置以在挂载的驱动器中创建文件/文件夹并将文件移动到挂载的驱动器。挂载命令似乎可以正常工作,但此后脚本似乎无法访问它。我尝试使用 -persistent 选项,但没有任何区别。我认为可能的唯一另一件事是该命令在函数中被调用。

在下面的示例中,$Domain、$Year 和 $Month 都在我的脚本中进一步定义。

function Mount_Drive {
    Clear-Host
    Write-Host "Mounting drive..." -NoNewLine -ForegroundColor Yellow
    New-PSDrive -Name "M" -Root "\\192.168.1.10\f" -PSProvider "FileSystem" -Credential $S_Creds
    Write-Host "Done" -ForegroundColor Green
}


function Make_Dir{
    Write-Host "Checking for directory structure..." -NoNewLine -ForegroundColor Yellow
    If(Test-Path "M:\$Domain\$Year\$Month"){
        Write-Host "[Exists] Done" -ForegroundColor Green
    }
    Else{
        Write-Host "[Does Not Exist]" -ForegroundColor Red
        Write-Host "Creating directory structure..." -NoNewLine -ForegroundColor Yellow
        ##################################
        New-Item -Path "M:\$Domain\$Year\$Month" -ItemType "directory" > $Null

        Write-Host "Done" -ForegroundColor Green
    }
}


Mount_Drive
Make_Dir

上面的代码会挂载驱动,但是 Make_Dir 函数不起作用。同样,如果我使用 -persistent 选项,我无法在 Windows 中看到驱动器。所以我认为它仅限于函数内的逻辑。

如果我注释掉挂载函数,而是通过 Windows GUI 挂载驱动器,脚本运行良好。

“姐妹”系统上的另一个脚本运行相同的挂载命令,但不使用函数;它像缩进一样工作。所以我希望有人能证实我的理论;因为我无法访问我试图让它运行的系统,除非在极少数情况下。<-- 是的,我知道这是一种可怕的做事方式,但它超出了我的控制范围:(

4

0 回答 0