Permission Denied when moving files using PowerShell script - powershell

Getting the below error while executing PowerShell script
+ CategoryInfo : PermissionDenied: (\\domain\tm1server.log:FileInfo) [Move-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : MoveFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.MoveItemCommand

You might have to use Copy-Item to move your file to the destination.
The documentation for Move-Item states:
The locations must be supported by the same provider. For example, it can
move a file or subdirectory from one directory to another or move a
registry subkey from one key to another. When you move an item, it is
added to the new location and deleted from its original location.

I had to add the sourcce directory as a PSDrive as well. Because the log in I used to the server didn't have WRITE acccess to the server.
So, I declared the sourcce directory as another PSDrive and used my ADMIN credential for creating it.
After that I was able to use move-item.

The folder specified in -path needs modify in addition to Read permission. Modify and write permission is needed on the -destination folder.

Related

failed copy file from path using powershell

I have this script:
$UserFile = Read-Host "Drag your file here"
Copy-Item -Path $UserFile -Destination .\input
I want user drag their file to console, so that the script can know the exact path of user file and copy user file to input folder. but i got this error
Copy-Item : Cannot find drive. A drive with the name '"C' does not exist.
At line:1 char:1
+ Copy-Item -Path $UserFile -Destination .\input
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
UPDATE:
It getting more weird guys, i think there is something wrong with my machine, i try to run the code on VM, and the code run with ZERO problem, and there's something different between my machine and VM, when i drag the file in my machine it will write like this "C:\path\to\my\file.something" but when i do that on my VM it write C:\path\to\my\file.something without "
UPDATE:
I just realized that i use different file, the file that i try on my machine is have white spaces in it file one.something but file that i use on VM don't have space file.something but i've tried to run the code like this
Copy-Item -Path "C:\path\to\my\file one.something" -Destination .\input
using " and it work. but that not what i want, i want user to drag their file no matter there is white space or not.
Try to add the -Prompt param
$UserFile = Read-Host -Prompt "Drag your file here"
*** drags file ***
Write-Host $Userfile ## should display the filepath
My guess is that this isn't an issue for you anymore and an extra " was entered into the terminal before dragging your file. For me, the code you've posted works without issue.
You might want to check out Cannot find drive. A drive with the name '"C' does not exist where the issue was the exact same thing.
Depending on the use of this script, you can trim/remove any characters before using and, in my experience, it is almost always better to use absolute paths instead of relative (here the ./input) unless you explicitly want to have the script be relative.

How to mount a SMB share to local folder - not only a drive letter - using powershell

Is it possible to mount a SMB share (or any other network share) to a local folder with powershell - apart from using "net use"?
I tried New-SmbMapping and New-PSDrive. But I get errors for both.
This is what I tried with New-SmbMapping
New-SmbMapping -LocalPath E:\MOUNT\pro\ -RemotePath \\host.example\projects
I get the specified device name is invalid and:
+ CategoryInfo : NotSpecified: (MSFT_SmbMapping:ROOT/Microsoft/...MSFT_SmbMapping) [New-SmbMapping], CimException
+ FullyQualifiedErrorId : Windows System Error 1200,New-SmbMapping
And for New-PSDrive, it seems not possible at all.
Thanks for any help!
You cannot use New-SMBMapping to create a mapping to a local folder - only to a drive letter. The "device name is invalid" error refers to the fact that E:\MOUNT\pro is not a valid device letter (it expects a single English letter).
To mount an SMB path into a local folder, you need to create a "junction point" - what Powershell calls SymbolicLink. For example:
New-Item -ItemType SymbolicLink -Path mytest -Target '\\computer\share'
The local path (./mytest in the above example) must not exist as New-Item needs to create it (this is unlike file system mapping in other operating systems).
Your current user must have access to the remote share, otherwise you'd get a non-helpful error message such as New-Item : Cannot find path '\\computer\share' because it does not exist. You can access shares for which you need to provide a user name and password by first loading the credential with net use before running New-Item (I don't know of Powershell way to do net use).

Get-ChildItem -path "\\Servername.net\FolderName\SubFolderName\" error with UNC path

I am using Get-ChildItem with a path to navigate to a common file share.
I was able to navigate to common file share from my local machine without any issues but when I deploy this code into the PROD server, there I am running into Cannot find path issue.
Power Shell Command:
Get-ChildItem "\\servername.net\abcd\"
Error:
Get-ChildItem : Cannot find path '\\servername.net\abcd\' because it does not exist.
At line:2 char:1
+ Get-ChildItem '\\servername.net\abcd\'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\\servername.net\abcd\:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Not sure if anyone experienced this issue and how to resolve these environmental issues.
Any help is greatly appreciated.
Power shell error
Based on your screenshot, I believe this to be a limitation of Get-ChildItem accessing UNC Paths while the current location is a SqlServer provider. I see a few options:
Map a PSDrive to the UNC path so that Get-ChildItem has a FileSystem provider to work with directly.
New-PSDrive -Name Z -Root "\\servername.net\abcd" -PSProvider FileSystem
Get-ChildItem Z:
Use a different method to retrieve files and directories.
[System.IO.Directory]::EnumerateFileSystemEntries("\\servername.net\abcd")
Use the location stack to go into a FileSystem. Then return to your previous PSDrive.
Push-Location C:
Get-ChildItem "\\servername.net\abcd"
Pop-Location
After evaluating the environments found that these UNC path issues are happening because of Domain differences.
My laptop and archive file share are residing in the same domain, and that's how archiving script was working without any issues.
Whereas the PROD server where I have deployed the script is residing in a different domain.
Resolved the issue by identifying another PROD server with the same domain name as the archive file share and deployed the script using task schedular.

Out-file export to a network location powershell

I am having an issue trying to output a file with PowerShell, I can export the file to the computer's local drives however when I want to export it to a network location it will not let me.
I receive the following error:
Access to the path '\\fmadt-prod-web5\e$\ftproot\customer\temp\SiteLists\Classic\Hosted1.txt' is denied.
+ CategoryInfo : OpenError: (:) [Out-File], UnauthorizedAccessException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
This is the code that I am using:
$list2 | Format-Table -a -Property "WebAppName", "Version", "State"| Out-File '\\fmadt-prod-web5\e$\ftproot\customer\temp\SiteLists\Classic\Hosted1.txt' -force
Is it possible to export to a network location? The user I am using has admin access to that location as well.
You can't use a UNC path, but you can map a PSDrive to that location and use that:
New-PSDrive -Name dest -Root \\fmadt-prod-web5\e$\ftproot\customer\temp\SiteLists\Classic -PSProvider FileSystem
Then:
| out-file dest:\Hosted1.txt
Use the -Credential parameter of New-PSDrive if you need to access the drive using alternate credentials.
you most certainly can output to unc paths, I regularly use those at work. This almost looks like you don't, or the account you are running the script as doesn't, have access to the directory.
E$ refers to the admin share on a server, try actually sharing that directory via windows shares or run the script using an account that is in the administrators group on the relevant server. Also, I always use double quotes for paths because then you can include variables -- force of habit :)

Open a Solution from the Package Manager Console

I'm trying to automate the process of opening a solution from source control.
I have VS12 open, but no solution or project open. Is it possible to change directories and then open a solution from the Package Manager Console?
(This is kind of beside the point, but in case there is a better way overall to do this) I'm trying to script this so that a powershell module installed via nuget could be run:
PM> Get-MyProject 'SomeName'
The module would then from pwd get the latest source cd into it and open the solution. My module can already get the source, but I'm not able to figure out how to open the solution in powershell. It seems like $dte should be able to do it, but I tried:
PM> $dte.Solution.Open('NugetTest.sln')
I get back the error
Exception calling "Open" with "1" argument(s): " could not be found. (Exception from HRESULT: 0x80030002 (STG_E_FILENOTFOUND))"
At line:1 char:1
+ $dte.Solution.Open('NugetTest.sln')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
The default directory for the package manager console (unless you have defined a profile where you change it) is %userprofile%. Probably not the place where your solution sits. If you write a powershell script and place it in your solution directory, then execute it from the Power Shell Console, you can get the path of your solution doing this:
$path = Split-Path -parent $MyInvocation.MyCommand.Path
You can create a solution like this:
$solution = $dte.Solution
$solution.Create("C:\Temp", "MySolution.sln")
The first argument indicates the directory where you want to create the solution, the second one is the name of the solution itself.
To open an existing solution:
$solution = $dte.Solution
$solution.Open("<path to your solution>")
If it doesn't work, your path is probably incorrect.