failed copy file from path using powershell - 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.

Related

Not able to unlock locked windows file folder

I have one utility provided by Dev, Which takes the firmware package as input and decrypts its content. After decrypts, it asks for a physical device to flash this decrypted content into that device. But before flashing, it creates one locked folder on my machine.
I am trying to unlock that locked window file folder.
Nothing happened when I tried to open it by double-clicking on the folder icon. Also, Write error occurred When I wanted to rename it using PowerShell.
PS C:\Users\RajNegi\AppData\Roaming> rename-item
cmdlet Rename-Item at command pipeline position 1
Supply values for the following parameters:
Path: C:\Users\abc\AppData\Roaming\vsc.{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0}
NewName: C:\Users\abc\AppData\Roaming\vsc
rename-item : Access to the path 'C:\Users\abc\AppData\Roaming\vsc.{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0}' is denied.
At line:1 char:1
+ rename-item
+ ~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\abc...f-00c04f60b9f0}:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
Note: I already set Full control in folder permissions.

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.

Permission Denied when moving files using PowerShell script

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.

Powershell script for copying and logging

I have search for similar answers to this and still I am going round in a circle(s).
I am new to any form of scripting so this is a bastardised script. The script is basically copying log files and data from locations to a remote server and making an append log each time it does it but for the life in me I cant get it to work over the network only local, by changing the $dirname = "D:\${env:computername}".
I would appreciate any feed back and help. This came about from a batch file I created and thought to try and progress in the dark arts.
The script is going to be scheduled to run task when a machines connects to the network.
thanks in advance
update
I get no output or error message from the log file at all no txt or data of any type, As for error messages I am trying to copy from local to server in a vm scenario and will not run, but if I apply this on the local machine it will copy c to d no problem. as I said complete novice
missing function body in function declaration
at line:2 char1
<<<<c:script\copy_log.ps1
+categoryinfo : parser error: (:) []. ParentContainsErrorRecordException
+FullyQualifiedErrorId : MissingFunctionBody
Apologies for format had to type it as I can c+p from the unit
UPDATE
figured out that the share to the other server was not shared correctly fixed this but the script still does not create a log file
function CopyLogFiles ($sourcePackage) { #used this syntax as I couldn't get anything else to work and took it from here
$dirName = "\\server\$sourcePackage" #server it is going to
if (!(Test-Path $dirName)) { mkdir $dirName }
Copy-Item -Path "C:\Program Files (x86)\ESS-T\$sourcePackage\Logs" -Destination $dirName -Recurse -Force
}
CopyLogFiles AppLauncher_V2.0.0.7
CopyLogFiles MMA_V2.0.0.12
CopyLogFiles MML_V2.0.0.4
CopyLogFiles SerialDataReader_V2.0.0.5
function Log-Write {
Param ([string]$LogString)
Add-Content $LogFile -value $LogString
}
$LogFile = "C:\Program Files (x86)\ESS-T\.log"
Don't reinvent the wheel. Copy-Item is convenient for small cases, but Windows has had robocopy included with every install since Windows 7 and it's faster, more robust, and has logging built in with the /log:FILENAME switch.
https://technet.microsoft.com/en-us/library/cc733145.aspx
Go ahead and test for the existence of your destination & create it manually in your PowerShell script, but leave the logging of the copy operation to robocopy.
Edit: You aren't creating the logfile because you don't define the logfile name until after the rest of your code runs.