Robocopy cannot use a PS-Drive name longer than one character - powershell

I have a UNC path mapped to a PSDrive.
PS Plong:\home\Personal\lit> Get-PSDrive
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
...
HKLM Registry HKEY_LOCAL_MACHINE
P 384.97 546.54 FileSystem \\RT-AC66U\files
Plong FileSystem \\RT-AC66U\files
Variable Variable
...
robocopy does not appear to understand a drive name longer than one letter. Appears to work with a one letter drive name. Is there any workaround?
PS Plong:\home\Personal\lit> robocopy Plong:\home\Personal\lit\ Plong:\home\Personal\lit\tt *.sql
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Wednesday, July 12, 2017 20:33:57
Source : \\RT-AC66U\files\home\Personal\lit\Plong:\home\Personal\lit\
Dest : \\RT-AC66U\files\home\Personal\lit\Plong:\home\Personal\lit\tt\
Files : *.sql
Options : /DCOPY:DA /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
2017/07/12 20:33:57 ERROR 161 (0x000000A1) Accessing Source Directory \\RT-AC66U\files\home\Personal\lit\Plong:\home\Personal\lit\
The specified path is invalid.
Waiting 30 seconds...

robocopy is not a PowerShell command and doesn't recognize PowerShell paths. You need to map the UNC path to a regular Windows (single-letter) drive for robocopy to be able to work with it.
You already seem to have the path mapped to the drive P: according to your Get-PSDrive output, so simply change your commandline to this:
robocopy P:\home\Personal\lit\ P:\home\Personal\lit\tt *.sql

Related

PowerShell: What kind of keyword ist env: ? Meaning of such a keyword?

For to display all environmental variables in PowerShell one uses:
Get-ChildItem env:
What is 'env:'?
It clear to me that it is some abbreviation for "environment". But what kind of abbreviation? What's the meaning of the colon at the end?
Your query is defined in the PowerShell help files:
About Automatic Variables:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.1
$env is a virtual drive for the above. Easily seen via the below and its purpose to allow access to the above:
# These are treated as normal filesystem drives, and you can create custom ones.
Get-PSDrive
<#
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
Alias Alias
C 5.04 34.83 FileSystem C:\ Scripts
Cert Certificate \
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
Variable Variable
WSMan WSMan
#>
Get-ChildItem -Path 'env:\'
<#
Using these in code requires the $ in front of each named variable to get the content/values. In your scripts, never name your custom variables the same as any of the below.
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\WDAGUtilityAccount\AppData\Roaming
CLIENTNAME 8fda9520-99a6-4
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME 0C092C31-6890-4
...
#>
If you do want to use the names above, as custom variables for other values, then you should provide a unique prefix. Say, your initials, so as to not have conflicts/errors.

Setting environment variables in powershell

In cmd, you could access cmd environment variables outside of cmd, but is there a way to accomplish that is powershell?
BACKGROUND:
In command-prompt (cmd.exe), there are environment variables like %COMSPEC% and %PROGRAMFILES% that you could use outside of cmd an din file explorer and other places (i. e. shortcuts) just by typing in %VariableName% somewhere (this is how I see a lot of people accessing their appdata folders) but I have yet to find a way to do it in powershell. In cmd, you could even define a variable with setx nd be able to use it outside of cmd. Ex:
setx testbat "%HOMEDRIVE%%HOMEPATH%\Desktop\Programs\test.bat"
Then you could use it like this in shortcuts:
%testbat%
Shortcut Wizard with cmd variable
However, I have not been able to replicate this with Powershell variables.
What I've tried:
I saw this and wondered if making an environment variable in powershell with this in mind could be possible:
$env:test = "C:\Users\Neko"
Then I restarted and tried using this both in cmd and in the search prompt (search prompt)
C:\Users\Neko>cd %test%
The system cannot find the path specified.
C:\Users\Neko>echo %test%
%test%
Sure enough, it didn't even show up in powershell when I did gci env: after the restart
Since this didn't work I deleted the variable and had an idea:
PS C:\Users\Neko> gci env:
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\Neko\AppData\Roaming
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME XXXXXXXXX
ComSpec C:\Windows\system32\cmd.exe
DriverData C:\Windows\System32\Drivers\DriverData
HOMEDRIVE C:
HOMEPATH \Users\Neko
LOCALAPPDATA C:\Users\Neko\AppData\Local
LOGONSERVER \\XXXXXXXXX
NUMBER_OF_PROCESSORS 8
OneDrive C:\Users\Neko\OneDrive
OneDriveConsumer C:\Users\Neko\OneDrive
OS Windows_NT
POWERSHELL_DISTRIBUTION_CHA... MSI:Windows 10 Pro Education
PROCESSOR_ARCHITECTURE AMD64
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 126 Stepping 5, GenuineIntel
PROCESSOR_LEVEL 6
PROCESSOR_REVISION 7e05
ProgramData C:\ProgramData
ProgramFiles C:\Program Files
ProgramFiles(x86) C:\Program Files (x86)
ProgramW6432 C:\Program Files
PSModulePath C:\Users\Neko\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\M...
PUBLIC C:\Users\Public
SystemDrive C:
SystemRoot C:\Windows
TEMP C:\Users\Neko\AppData\Local\Temp
test C:\Users\Neko
TMP C:\Users\Neko\AppData\Local\Temp
USERDOMAIN XXXXXXXXX
USERDOMAIN_ROAMINGPROFILE XXXXXXXXX
USERNAME Neko
USERPROFILE C:\Users\Neko
windir C:\Windows
WSLENV WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID XXXXXXXXX
WT_SESSION XXXXXXXXX
PS C:\Users\Neko> del "env:test"
PS C:\Users\Neko> gci env:
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\Neko\AppData\Roaming
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME XXXXXXXXX
ComSpec C:\Windows\system32\cmd.exe
DriverData C:\Windows\System32\Drivers\DriverData
HOMEDRIVE C:
HOMEPATH \Users\Neko
LOCALAPPDATA C:\Users\Neko\AppData\Local
LOGONSERVER \\XXXXXXXXX
NUMBER_OF_PROCESSORS 8
OneDrive C:\Users\Neko\OneDrive
OneDriveConsumer C:\Users\Neko\OneDrive
OS Windows_NT
Path C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPo...
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
POWERSHELL_DISTRIBUTION_CHA... MSI:Windows 10 Pro Education
PROCESSOR_ARCHITECTURE AMD64
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 126 Stepping 5, GenuineIntel
PROCESSOR_LEVEL 6
PROCESSOR_REVISION 7e05
ProgramData C:\ProgramData
ProgramFiles C:\Program Files
ProgramFiles(x86) C:\Program Files (x86)
ProgramW6432 C:\Program Files
PSModulePath C:\Users\Neko\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\M...
PUBLIC C:\Users\Public
SystemDrive C:
SystemRoot C:\Windows
TEMP C:\Users\Neko\AppData\Local\Temp
TMP C:\Users\Neko\AppData\Local\Temp
USERDOMAIN XXXXXXXXX
USERDOMAIN_ROAMINGPROFILE XXXXXXXXX
USERNAME Neko
USERPROFILE C:\Users\Neko
windir C:\Windows
WSLENV WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID XXXXXXXXX
WT_SESSION XXXXXXXXX
I then checked to see if the cmd set command beared the same results as gci env: and it did, so I then tried to use setx to see if it appeared in powershell as well:
setx test "Test"
And after a restart, it did
PS C:\Users\Neko> $env:test
Test
I realized that the variables all were stored in the registry and that I could edit the registry of course with:
Set-Itemproperty -path 'HKCU:\Environment' -Name 'Test' -value 'Test'
But it doesn't feel the same as something like setx
CONCLUSION:
I want to learn if there is a true-powershell-esque command that can define environemnt variables that I can use outside of powershell like setx can in cmd. I am not looking for full scripts just commands or something like $env:variable = "value".
UPDATE:
Yes, you can create a function to do this as well, technically a one-liner possibly, not what I'm looking for. I want to be able to create environment variables in powershell with built in cmdlets. Something "true-powershell"
Powershell itself provides these methods to interact with environment variables:
The Environment provider drive
The Item cmdlets
The .NET System.Environment class
On Windows, the System Control Panel
So, to set a persistent environment variable (PSModulePath) to a value $newpath at machine level you might use
[Environment]::SetEnvironmentVariable("PSModulePath", $newpath, 'Machine')
To get an environment variable you might use
$path = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
or the variable syntax
$Env:PSModulePath
Here's the documentation.

Windows Powershell Environment Variables

I tried but did not find an answer :
How do I get the current user home in Windows PowerShell?
System variables (those that you would address in Batch as %varname%) are accessible in PowerShell as $env:varname. You can list the system variables that are visible to your PowerShell session with Get-ChildItem -Path Env:.
In Windows, it looks like $env:userprofile or $env:homepath without the drive.
dir env: | where value -match admin
Name Value
---- -----
APPDATA C:\Users\admin\AppData\Roaming
HOMEPATH \Users\admin
LOCALAPPDATA C:\Users\admin\AppData\Local
OneDrive C:\Users\admin\OneDrive
Path C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Wind...
PSModulePath C:\Users\admin\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerSh...
TEMP C:\Users\admin\AppData\Local\Temp
TMP C:\Users\admin\AppData\Local\Temp
USERNAME admin
USERPROFILE C:\Users\admin
Try this:
[Environment]::ExpandEnvironmentVariables("%UserProfile%")

Powershell execution policy remotesigned contradiction?

please look at the following URL: URL
Now it says the following about downloaded scripts:
"Runs scripts that are downloaded from the Internet and not signed, if the scripts are unblocked, such as by using the Unblock-File cmdlet."
I just downloaded a script from the technet gallery (PS2EXE) and I could run the test script that was included just fine without using the Unblock_file cmdlet. What is going on? Am i misunderstanding what Microsoft is telling me or is this a glitch?
help unblock-file:
Internally, the Unblock-File cmdlet removes the Zone.Identifier alternate data stream, which has a value of "3" to indicate that it was downloaded from the Internet.
The idea of a file being "remote" or "coming from the internet" is data on your local computer filesystem which has to be put there by the tool that downloads the file, it's not included in the file during the download.
If you downloaded a file through Internet Explorer, maybe FireFox, Invoke-WebRequest, these will add it. If you download with something else, the tool might not add this alternate stream.
See how it behaves:
# Show folder is empty
PS C:\temp\> Get-ChildItem
# Make a test script which prints Hello World, and run it
PS C:\temp\> "'Hello World'" | Set-Content -Path .\test.ps1
PS C:\temp\> .\test.ps1
Hello World
# Show the file exists
PS C:\temp\> Get-ChildItem
Directory: C:\temp\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 01/08/2018 22:07 15 test.ps1
# Add the Zone Identifier alternate data stream
PS C:\temp\> "[ZoneTransfer]`nZoneId=3" | Set-Content -Path 'test.ps1' -Stream 'Zone.Identifier'
# Show that it doesn't appear in a normal directory listing:
PS C:\temp\> Get-ChildItem
Directory: C:\temp\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 01/08/2018 22:08 15 test.ps1
# Show how it blocks the file from running
PS C:\temp\> .\test.ps1
.\test.ps1 : File C:\temp\test.ps1 cannot be loaded. The file C:\temp\test.ps1 is not digitally signed. You cannot
run this script on the current system. For more information about running scripts and setting execution policy, see
about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\test.ps1
+ ~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
# Show file content
PS C:\temp\> Get-Content -Path .\test.ps1
'Hello World'
# Show alternate data stream content
PS C:\temp\> Get-Content -Path .\test.ps1 -Stream 'Zone.Identifier'
[ZoneTransfer]
ZoneId=3
# Unblock-File removes this alternate stream
PS C:\temp\> Unblock-File .\test.ps1
# Script runs again
PS C:\temp\> .\test.ps1
Hello World
So the main question is, if you run Get-Content file.ps1:Zone.Identifier and see the ZoneId is 3 and can still run the script, and Get-ExecutionPolicy is RemoteSigned, then you have something odd going on.
But my guess is the download tool did not add this data, so the file looks just like a locally created one.
NB. RemoteSigned is not intended to be a security feature, it's intended to be a "help guard against accidentally running scripts before reading them and deliberately choosing to run them" check, like an "are you sure?" box, not like a password prompt.

Robocopy appears to do nothing when /MT flag is used

This command works:
robocopy \\build7\d$ d: *.* /mir /XD "System Volume Information" /XD "$Recycle.Bin"
But when I try adding the /MT flag:
robocopy \\build7\d$ d: *.* /mir /XD "System Volume Information" /XD "$Recycle.Bin" /MT
The only output I get is
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Thu Apr 09 09:11:27 2015
Source : \\build7\d$\
Dest : D:\
Files : *.*
Exc Dirs : System Volume Information
$Recycle.Bin
Options : *.* /S /E /COPY:DAT /PURGE /MIR /MT:8 /R:1000000 /W:30
------------------------------------------------------------------------------
It gives me that output, and then just sits there.
I tried using the /LOG flag to redirect the output to a file, but it still just sat there with the same output, only in a file.
Is it doing something in the background that I can't see, or am I doing something wrong?
In my experience, what you are seeing is normal. When Robocopy runs using multiple threads (/MT), the output is limited to exceptions (new file, extra dir, error, etc...). It will not show you the directory names with the file count that you are probably used to seeing. That means that if the target is largely in-sync with the source, you will not see much output.
I don't know exactly why this is the case. I assume that Robocopy handles the directory walking differently when there are multiple threads involved. This difference probably makes the normal output impossible (or meaningless) to produce.
When I am running Robocopy with multiple threads, I often use the Disk tab in the Windows Resource Monitor to see what Robocopy is doing. How to Use Resource Monitor