Powershell - Log Off Script to copy two files - powershell

Very much new to Powershell scripting. Usually am running scripts I find somewhere else...not trying to create my own. I am using Remote Desktop Services. I have a batch file that will copy two directories to a userpath on log on. When the user logs out, I only want to copy 2 specific files back to the file share. Here is the powershell script I (along with help from another online community) came up with.
I would like to assign this to a Log Off Script for the users. It fails at $unc. More information:
W drive points to the users directory on the File Share.
I have also tried to path it as \\fileshare\Share\$username
CODE:
function copydir ($user){
$userpath = $env:HOMEPATH
$unc = W:
copy-item $userpath\Data\App\DMS\DMS.accde $user\Data\App\DMS\ -recurse
copy-item $userpath\Dynamics\GP2010\dex.ini $user\Dynamics\GP2010\ -recurse
}
$username = $env:username
copydir $username*
Error:
The term \\fileshare\Share\$username is not recognized as the name of a cmdlet, function, script file....
So, obviously it doesn't like the $UNC parameter. I tried to run it with just W: instead since that points to the User Profile on File Server and get a similar error.
But the basic premise I'm trying to accomplish is to copy Data\App\DMS\DMS.accde and Dynamics\GP2010\dex.ini to a users roaming profile on a file share.
Thanks!

Can it be that you need to have the double slashes at the front for your UNC path?
$unc = "\\fileshare\Share\$user"

Related

PowerShell - can´t find long path but file exists

I have an issue while trying to run a PowerShell script.
When i try to obtain a file content (ex. Get-Content ".\d\e\file.xml") using a script a few directories bellow this .xml it works.
But, if i run this script from a different directory to get the file content (ex. c:\users\x\desktop), it will not be able to read it. I have tried Set-Location -Path "C:\a\b\c\ so it can get by relative path.
This is the error that I´m having:
Cannot find path 'C:\a\b\c\d\e\file.xml' because it does not exist. (path has 263 characters, too long)
Powershell Version : 5.1.18362.628

Powershell Dot Slash .\ Starts at the root of a drive

Note: I'm using the built-in PowerShell ISE as my environment
I got a funny issue with dot slash on Powershell. All of my scripts run from a certain folder and there are subfolders that contain data that is needed for them to run.
For example, my scripts are saved at c:\users\chris\posh
Most of the time, I will call input and send output to subfolders like this...
c:\users\chris\posh\inputs
c:\users\chris\posh\output
Therefore I'll have scripts examples that look like this for inputs and outputs:
$hbslist = Get-Content .\inputs\HBS-IP.txt
write-output "$($lat),$($long)" | Out-File .\Outputs\"LatLong.csv" -Append
Lately, when I run the scripts, it cannot locate my files or exe's that I call on. That's because it's trying to look at P:/ instead of c:\users\chris\posh when using .\
Powershell also starts in my P:\ (mapped share drive) for some reason and I cannot figure out as to why my PC is running this way.
It might be a policy on your machine which changes your home directory. You can check the home directory with:
echo $env:HOME
This happens often on corporate machines. If you want to set it back for your powershell environment, you can set it in your profile.ps1.
This is typically stored at:
c:\Users\<Name>\Documents\WindowsPowershell\profile.ps1

How can I set up a default powershell profile for all my co workers at work as simple as possible?

What do I want to achieve?
I have one ps1 file that has all of my functions inside. In the first step I want to convert it into a ps module. Then I want to have the following:
Colleague gets a script or bat he has to run ONCE. This will set his Modules Environment path $Env:PSModulePath to a path on a network drive everyone has access to
Copy and paste a custom profile.ps1 into the users %userprofile%\Documents\WindowsPowershell that imports the module
Every user should now have the powershell scripts I made available in their shell
How I tried to solve it
The way me and a colleague have set it up in the past is with this:
(we have a profile.ps1 file that does the following):
#set path for profile_loader.ps1
$path = "\\server\share\folderwithscripts";
#call profile_loader.ps1
. "$path"
Then this profile_loader.ps1 baiscally just loads tons of scripts (ps1 files) like this:
. "\\server\share\pathtoanotherscript.ps1
Line after line.
I don't like it and it is too complicated for my 25 other colleagues I want to set up in the future.
Question
What is the best way to achieve this? A good old .bat file that copy and past the ps1 file into their userprofile? Or is there a better way?
As someone who had their $profile wiped and set to a "company default", for the love of god, don't.
If you have to, then I suggest just creating a profile you want everyone to have with all your modules in a shared location, like your securely locked down Sysadmin folder.
Do psedit $proile.AllUsersAllHosts on your machine, modify that, then make a text file with all the hostnames you want to destroy with your own forced profile. Throw this in there to make it import your modules by default.
# Checks your server share for any PSM1 files, could change to include PS1 as well I suppose. Long name because its in a $Profile so ¯\_(ツ)_/¯
$ModulePathWithLongNameBecauseSomeoneMayUseThisInAnActualScript = Get-ChildItem -file -Recurse "\\server\share\" -Include "*.psm1"
# Sets module path for other adhoc module calls if they dont want to restart their Powershell
$env:PSModulePath = $env:PSModulePath + ";\\server\share\"
# Imports all PSM1 files from the ModulePath*
Foreach($psm in $ModulePathWithLongNameBecauseSomeoneMayUseThisInAnActualScript){
Import-Module "$($ModulePath.FullName)"
}
Run this on your machine to deliver your soul crushing $profile to your colleagues who may have had their own setup.
# Get a list of machines that your staff will use and throw them into a txt or csv etc.
$PCsForForcedProfile = Get-Content "\\server\share\PleaseNo.txt"
Foreach($Colleague in $PCsForForcedProfile){
Copy-Item "C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1" "\\$Colleague\C$\Windows\System32\WindowsPowerShell\v1.0\" -force
}

PowerShell - Trouble copying items remotely with invoke-command and copy-item

I have hundreds of computers on domain I am responsible for and I want to copy a file from a file server to their hard drives locally. If I script this to initiate the copy from my (separate) computer it works just fine via something like;
Copy-Item -LiteralPath \\FILESERVER01\Share\Files\Win7_64\File_to_copy.txt -Destination \\WORKSTATION01\c$\Users\USER\Desktop\ -Force }
However I'd like the script to initiate a copy from the file server to the workstation on the workstation via invoke-command so this isn't done on my machine using something (I assume is) like this:
Invoke-Command -ComputerName WORKSTATION01 -ScriptBlock { Copy-Item -LiteralPath \\FILESERVER01\Share\Files\Win7_64\File_to_copy.txt -Destination \\WORKSTATION01\c$\Users\USER\Desktop\ -Force }
But doing this consistently gives me the error citing the source as the issue - Cannot find path '\FILESERVER01\Share\Files\Win7_64\File_to_copy.txt' because it does not exist. I have access to this network shared path from all machines (via Domain Admin rights), so I believe this is something like a syntax error or problem with the way the UNC path is being resolved but despite reviewing several topics on this I just can't determine what I'm doing wrong with this since I'm using the same UNC paths in the same commands outside the invoke-command cmdlet (and from a PowerShell session on WORKSTATION01) with no issues.
I've tried reviewing and attempting solutions from this link, and also this link. But I can't tell if its a slightly different use case or if I just cant figure out how to adjust the syntax to make it work for me but the Cannot find path error persists
I've tried using New-PSDrive, prefixing the UNC path with "FileSystem:" and even taking the explicitly shared folders out entirely and using only the full path using administrative shares but I just seem to be missing something.
Can anyone shed some light on what I'm missing here?

Running a cmd in powershell referencing a UNC path

I’m in the process of creating a powershell script to check OU users against users already configured for file share archiving but I’ve hit a stumbling block. I can query AD to get a list of users per OU and their home directories, dumping all of the details out to text files for logs and basing subsequent queries on. Once I have these details I try to run a dos command, (Enterprise Vault) Archivepoints.exe passing variables to it. The command would usually be :
Archivepoints.exe find \\fopserver045v\ouone_users$
When I try to run the following code I get an error.
$app="D:\Enterprise Vault\ArchivePoints.exe"
$EVArg = "find"
$VolLine = "\\fopserver045v\ouone_users_r$"
Invoke-Item "$app $EVArg $VolLine"
Invoke-Item : Cannot find path 'D:\Enterprise Vault\ArchivePoints.exe find \fopserver045v\ouone_users_r$' because it does not exist.
At first I thought it was missing the first backslash of the UNC path that was causing the issue but I'm no longer sure.
The script and command run on the EV server and the UNC bath doesn't actually go to the server, it's only a reference path within EV so it's not a credentials issue.
I need to be able to log the output to file too if possible.
What should the code look like and should I be using invoke-command or Invoke-Expression instead ?
Thanks
Don't use Invoke-Item. External commands should be run using the call operator (&). You can use splatting for the argument list.
$app="D:\Enterprise Vault\ArchivePoints.exe"
$arguments = "find", "\\fopserver045v\ouone_users_r$"
& $app #arguments