open folder from Network with Powershell - powershell

I would like to open a txt.File from a sharedFolder with Powershell. The problem is, that it has to run whether an user is logged on or not. I'm looking for something like net-use.
The Program should also check, whether the psDrive exists or not.
Is it possible if I do that like this?
new-psdrive -name Z -psprovider FileSystem -root \\vcs.view
It works like that:
I map and then I check whether the file exists:
#mapping
try
{
new-psdrive -name Z -psprovider FileSystem -root $ShareFolder
}
catch
{
echo "WriteMessageTOAdmin ERROR!!"
exit
}
$folderPath = "Z:\users.txt"
if(!(test-Path -path $folderPath))
{
echo "WriteMessageTOAdmin ERROR!!"
exit
}

You don't need to map a network share to open a file on a network share:
Get-Content \\server\sharename\foo.txt
Works just fine as does using Test-Path on a UNC path e.g.
Test-Path \\server\sharename\foo.txt
Is there a reason you need to map the share to a local drive?

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 remove mapped network drive with Powershell?

I am trying to create and remove a new mapped network drive using PowerShell.
It creates the mapped drive, however I can't seem to remove the mapped drive. The error message I receive is:
Dir : Cannot find path 'C:\Windows\system32\P' because it does not exist.
New-PSDrive -Name "P" -Root "\\VM-Blue-Robin\Testing" -Persist -PSProvider "FileSystem"
#Get-PSDrive P | Remove-PSDrive
#Remove-PSDrive -Name P -Force
#Remove-PSDrive P, Z
All Google and Stack Overflow has suggested to me thus far is using the commands that I have previously commented out. I am unsure of what I am doing wrong but had a feeling it could be done to the location of my files perhaps?
All help would be greatly appreciated!
The error is because you're running dir P instead of dir P: You need the : to signify a drive not a folder.
dir (which in Powershell is a actually an alias for Get-ChildItem) can read multiple areas of the OS so you need to be more specific with what you tell it.
Examples:
File system: Get-ChildItem C:
Registry: Get-ChildItem HKCU:
Certificate Store: Get-ChildItem cert:
Whilst with Get/Remove-PSDrive commands you are specifically telling it you want a "FileSystem" drive so it knows that Name is a drive letter.
With regards to removing the drive, either of the two commands you've listed will work fine:
New-PSDrive -Name P -Root "\\VM-Blue-Robin\Testing" -Persist -PSProvider "FileSystem"
Get-PSDrive P | Remove-PSDrive
Remove-PSDrive -Name P -Force

Empty folders when using Copy-Item Network Drive to Local Drive

I'm currently using this Powershell script
Function Copy-ItemUNC {
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files';
}
When I run the script it creates the folders but there are no subfolders within them.
Second problem I have is that I have to manually open Windows Explorer and type in the path to connect to it first in order for this script to even run. Is there a way to fix that too?
It's because you are not asking for subdirs, as that requires you to do this.
Copy-Item -Recurse
-Recurse <SwitchParameter>
Indicates that this cmdlet performs a recursive copy.
Required? false
Position? named
Default value False
Accept pipeline input? False
Accept wildcard characters? false
(Get-Command -Name Copy-Item).Parameters.Keys
Get-Help -Name Copy-Item -Full
Get-Help -Name Copy-Item -Examples
This command copies the mar1604.log.txt file to the C:\Presentation directory. The command does not delete the original file.
Example 2: Copy the contents of a directory to another directory
PS C:\>Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse
So, this line should be like this..
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files' -Recurse
You also do not really need this..
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
... based on what you are after. Especially since you are not using it anywhere in your code. That semi-colon is also not needed.

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.