Using a mapped drive as a variable - powershell

We're looking at swapping a file server with a newer one. The new one will have a new IP and name.
My aim is to remove the mapped drive our users have for the current server and replace it with a new one. The problem is our users have it on different letters. So I need to run a script that will replace the UNC path regardless of drive letter.
My script so far can find me the drive letter, but it's not removing the mapping.
--
Get-PSDrive | ForEach {
If ( $_.DisplayRoot -eq '\\OLDSERVER\PATH' ) {
Remove-PSDrive -Name $_.Name
New-PSDrive –Name $_.Name –PSProvider FileSystem –Root "\\NEWSERVER\PATH" –Persist
}
}

Powershell cannot remove a mapping it didnt put in. I've changed it to a net-use command and it works.

Related

Powershell Dynamic path location for files

I'm trying to make a script that installs apps etc. the script contains 7 ps1 scripts and they are linked together but when I move the folder the script won't work since the path changed is there a way so I can always have the right path?
& 'Z:\Windows installatie\Scripts\Menus\Apps.ps1'
this is when it's from a USB but the drive letter always changes.
I tried using a wild card but that didn't work.
& '*\Windows installatie\Scripts\Menus\Apps.ps1'
If you would like to test for the existence of a folder or file under an unknown drive letter, and you know the path is going to be unique enough, then you could just test for it by iterating through Get-PSDrive -PSProvider FileSystem.
$Drive = Get-PSDrive -PSProvider FileSystem | Where-Object { Test-Path ($_.Root + "path\to\myScript.ps1") }
if ($null -ne $Drive -and $Drive.Count -eq 1) {
& (Join-Path -Path $Drive.Root -ChildPath "path\to\myScript.ps1")
}
Not incredibly elegant, but will do the job.
If you know that you only have one USB mounted at a time, then you could also check for details about the drive that is a USB.

Using variables within quotes in a script to map network drives

We're logging network drive mappings to a log file on our laptops. And I wanna make a script that takes these log files and maps the logged drives for the user.
Example log script:
K: -> \\server01\folder
Y: -> \\server02\publicfolder
I already have a partial script, but it looks like it doesn't work.
$logfile | foreach {
$log = $_ -split ' -> '
# Write-Host $log[0]
# Write-Host $log[1]
$Networkpath = $log[1]
$DriveLetter = $log[0] -replace ":*",""
New-PSDrive -Name `"$($DriveLetter)`" -PSProvider FileSystem -Root `"$($Networkpath)`" -Persist -Scope Global
}
When I run the above script, I'm getting errors "New-PSDrive : When you use the Persist parameter, the root must be a file system location on a remote computer.".
I'm assuming that this is because the quotes around the $DriveLetter and $Networkpath variables aren't handled correctly.
But I'm not sure how to do it properly. Been trying multiple solutions from Google, but none seem to work :(

How to create a folder on C: drive of several PCs in a network using powershell

I need to create a folder called "logs" on the C: drive of all the machines in my organisation. How can i do this using PowerShell?
I have a script to create the "logs" folder however i need a way to do this on more than 100 machines in Active Directory.
Any advice?
This is the script i'm using to create the folder on my machine:
New-Item -Path c:\Logs -ItemType directory -Force
Is there a way i can apply this script to my entire organisation?
Thanks.
This is not a duplicate as i am trying to do this in a domain environment to all the machines in my organisation and not just one remote server.
Since you said it was a domain environment, you don't need to use remote execution, you can access the computer's hard disks remotely.
Get-ADComputer -Filter * | %{ $logsPath = "\\$($_.Name)\c$\Logs"; if ((Test-Path $logsPath) -eq $false) { New-Item -Path $logsPath -ItemType Directory } }
You will want to filter the output of Get-ADComputer or it will apply to every computer in your domain including the servers.

Using Powershell to test-path on a network drive

I can't get the Test-Path cmdlet to find a folder on a remote system's additional drive.
The following works for the system drive.
Test-Path -PATH '\\ServerName\C$\FolderName'
The next example always returns false.
Test-Path -PATH '\\ServerName\D$\FolderName'
I have verified the path and its spelling. Test-Path will find the path if I remote to the computer and run
Test-Path -PATH 'D:\FolderName'
I must be missing something. Will someone please enlighten me?
You need specified the UNC path in this way.
$path = 'filesystem::\\servername\D$\FolderName'
If(Test-path -Path $path){
Your code....
}
https://blogs.technet.microsoft.com/heyscriptingguy/2013/01/13/powertip-test-the-presence-of-a-remote-share-by-using-powershell/
Chances are the d-drive in this instance is not a logical disk on ServerName but a remote drive...
The Get-PSDrive CmdLet will return the details of the various drives available to your machine.
Get-PSDrive -PSProvider FileSystem will return, as you guessed it, file system drives! Pay particular attention to the Root property as this will show whether the drive in question is logical or remote.
You need to run this CmdLet when logged on the box, or alternatively you can use the Invoke-Command something like so:
Invoke-Command -ComputerName "ServerName" -Credential $(Get-Credential) -ScriptBlock {
Get-PSDrive -PSProvider FileSystem
}
You mention in one of the comments that it is a thumbdrive. Thumbdrives are not administratively shared by windows and therefore that share does not exist.
If you create a folder on the thumbdrive, such as d:\test and share that as "test", then you can test-path to "\servername\test" and get a good result.

Backup GPOs basing on GPO DisplayName rather than Id

So I am backing up all the GPOs of a Domain Controller and I noticed that the way Backup-GPO cmdlet backs up the GPOS is so unfriendly. By default it creates a folder for every single GPO named after the "ID" which doesn't even match its "GPOID/GUID".
Here is an example, I will just backup a specific GPO:
backup-gpo -guid ff8de365-0842-46ab-9ac7-64ebd8dd4614 -path C:\
DisplayName : N12 Workstation Policy
GpoId : ff8de365-0842-46ab-9ac7-64ebd8dd4614
Id : dd33c220-bac8-4ebd-a9d9-7729fcea9c38
BackupDirectory : C:\
CreationTime : 20/10/2015 17:41:43
DomainName : martyn.local
This is the backup folder name that is created after issuing the previous command:
{DD33C220-BAC8-4EBD-A9D9-7729FCEA9C38}
If I try to Backup all the GPOs I get a folder for every single GPO. Is there any way of naming these folders basing on the GPO DisplayName rather than that unfriendly string?
This is what I would like to get:
N12 Workstation Policy
The reason why I want to do it like that is because if I want to re-import a single GPO in the future and I don't remember the name of the GPO how am I supposed to know which is the right GPO backup folder to import if I am using that awful name?
Thanks
Backup each GPO to separate subfolders of your backup folder:
$invalidChars = ':\\/' + [RegEx]::Escape(-join [IO.Path]::InvalidPathChars)
$backupDir = 'C:\backup'
Get-GPO -All | ForEach-Object {
$name = $_.DisplayName -replace "[$invalidChars]", '_'
$gpoDir = Join-Path $backupDir -ChildPath $name
New-Item $gpoDir -Type Directory | Out-Null
Backup-GPO -Guid $_.Id -Path $gpoDir
}
The replacement is to remove characters that are invalid in a path from the name (the creation of the subfolder would fail if the name of the GPO contained for instance a >). As #briantist suggested in his comment it's better to derive the list of invalid characters from the respective IO.Path property than maintain the list by hand. You may need to manually add other problematic characters, though, specifically :. The colon is used for accessing alternate data streams, so it's technically a valid character in a path, but PowerShell doesn't support it.