PSDrive cmdlets and scope
Ieri am scris despre network drive map via Powershell. Ce mi-a scapat si am aflat si eu dupa, e scopul comenzilor care sincer mi se pare putin ciudat.
Daca rulezi dintr-o sesiune powershell si folosesti -Persist, o sa vezi maparea in Explorer. Dar daca rulezi New-PSDrive dintr-un script, in momentul in care scriptul isi termina executia, drive-ul o sa dispara din Explorer. Asta pentru ca scopul scripului este local (el insusi).
Ca si fix trebuie sa folositi –Scope Global in comenzile cu New-PSDrive:
Daca citeam si documentatia inainte sigur aflam:
- Mapped network drives: You can use the Persist parameter of New-PSDrive to create Windows mapped network drives. Unlike temporary Windows PowerShell drives, Windows mapped network drives are not session-specific; they are saved in Windows and they can be managed by using standard Windows tools, such as File Explorer and Net Use. Mapped network drives must have a drive-letter name and be connected to a remote file system location. When your command is scoped locally (no dot-sourcing), the Persist parameter doesn’t persist the creation of a PSDrive beyond the scope in which the command is running. If you are running New-PSDrive inside of a script, and you want the drive to persist indefinitely, you must dot-source the script. For best results, to force a new drive to persist indefinitely, add the Scope parameter to your command, and set its value to Global.
PS: Am observat si cateva cazuri ciudate. Sunt momente cand Explorer nu isi face refresh si cateodata e nevoie sa restartezi sistemul (posibil sa mearga si doar cu un restart de proces).