envirionment vairbales and scope - powershell

This script is supposed to create some new drives but after i run it they dont exist, any ideas why? totally stuck....
Thanks in advance
Function New-Drives {
Param()
New-PSDrive -Name AppData -PSProvider FileSystem -Root $env:Appdata
New-PSDrive -Name Temp -PSProvider FileSystem -Root $env
$env:TEMP=Join-Path -Path C:\Windows\Temp
$mydocs=Join-Path -Path $env:userprofile -ChildPath Documents
New-PSDrive -Name Docs -PSProvider FileSystem -Root $mydocs
}
DIR temp: | measure-object –property length -sum
New-Drives

you need to add the -Persist parameter to your New-PSDrive calls. otherwise it will only create the drive within your powershell session.

Related

Why is Get-ChildItem inside a Function Access denied after correct New-PSDrive? (It works outside of the function scope.)

This doesnt work
function ImportProcess{
param(
[Parameter()]
[string]$XMLPath
)
$sourcePath = $XMLPath
$DestPath = split-path "$sourcePath" -Parent #this gives format without slash at and and makes powerShell *very happy*
write-host "connecting to Dir"
New-PSDrive -Name "Drive" -PSProvider FileSystem -Credential $global:cred -Root "$DestPath" | Out-Null
write-host "done"
write-host "getting all xml files"
$temp1 = Get-ChildItem -Path $sourcePath
}
I want to put this inside a function and access the files in the path and this below works. I got this methode from using credentials to get-childitem on other server
$sourcePath = "path"
$DestPath = split-path "$sourcePath" -Parent #this gives format without slash at and and makes powerShell *very happy*
write-host "connecting to Dir"
New-PSDrive -Name "Drive" -PSProvider FileSystem -Credential $global:cred -Root "$DestPath" | Out-Null
write-host "done"
write-host "getting all xml files"
$temp1 = Get-ChildItem -Path $sourcePath
I tried different scopes in New-PSDrive and set-location before get-childitem, aswell as setting the Path to the Drive name + needed subdir
i removed the part where i get the credentials, because all works but as soon as i put the process in a function i get "Get-ChildItem : Access is denied"

send file to remote folder powershell

this is what i would use for copying without asking for password, because i want to schedule this script
$Source = "d:\test\myZipFile.zip"
$Dest = "\\REMOTE_ip\D$\test"
$Username = "Administrator"
$Password = ConvertTo-SecureString "PasswordOFRemotePC" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)
New-PSDrive -Name J -PSProvider FileSystem -Root $Dest -Credential $mycreds -Persist
Copy-Item -Path $Source -Destination "d:\test\myZipFile1.zip"
But it gives me error
New-PSDrive : The network resource type is not correct At D:\test\copy.ps1:7 char:1
New-PSDrive -Name J -PSProvider FileSystem -Root $Dest -Credential $m ...
+ CategoryInfo : InvalidOperation: (J:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
Going by the title of your question, I think you are overdoing this.
In your scheduled task, decide which user account should be running the script. That user should have Read access to the machine's local D:\Test folder and must have Write/Modify permissions to the remote path \\REMOTE_ip\D$\test.
You can set the task so that particular user does not even have to be logged in.
The executable to run is PowerShell.exe and the arguments are -File <PathToTheScript.ps1>
(for that, the task-running user must have Read & Execute permissions on the location of the script file)
The script itself can then be as simple as:
$Source = "d:\test\myZipFile.zip"
$Dest = "\\REMOTE_ip\D$\test"
Copy-Item -Path $Source -Destination $Dest -Force

Enumerate URL Scheme form powershell

Hey im trying to enumerate URL schemes in the registry but unfortunetly this is bit harder than i thought
So first of all
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
$REGPATH = "HKCR:\"
Get-ChildItem "$REGPATH"
Hive: HKEY_CLASSES_ROOT
Name Property
---- --------
* AlwaysShowExt :
ConflictPrompt : prop:System.ItemTypeText;System.Size;System.Date
Modified;System.DateCreated
ContentViewModeForBrowse : prop:~System.ItemNameDisplay;System.ItemTypeText
;~System.LayoutPattern.PlaceHol
der;~System.LayoutPattern.PlaceHolder;System.Dat
eModified;System.Size
ContentViewModeForSearch :
[....]
ss
AcroAccess.AcrobatAccess.1 (default) : AcrobatAccess Class
acrobat URL Protocol :
(default) : URL:Acrobat Protocol
acrobat2018 URL Protocol :
(default) : URL:Acrobat Protocol
I tried to filter it out to just get only custom schemes but with no good result:
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
$REGPATH = "HKCR:\"
Get-ChildItem "$REGPATH" | Where-Object {$_.Property -Match "^(default) : URL:.*"}
at this point i think it is the easiest way:
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
$REGPATH = "HKCR:\"
Get-ChildItem "$REGPATH" >> temp.txt
Select-String -Path .\tmp.txt -Pattern 'URL:'

Set registry key to open notepad++

I tried running this script using the ISE, and I also tried to run it on the command line as administrator. It freezes at the "Remove-ItemProperty" line. I've tried to remove that step, but then it freezes at the next step "Set-ItemProperty". It looks like the New-Item lines are working fine.
if (Test-path "HKCR:\")
{
}
else
{
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Unzip "npp.6.7.5.bin.zip" “C:\Notepad++”
New-Item -Type String "HKCR:\*\shell\Open With Notepad++"
New-Item -Type String "HKCR:\*\shell\Open With Notepad++\command"
Remove-ItemProperty "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)"
Set-ItemProperty "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)" -value "C:\\Notepad++\\notepad++.exe %1"
Any suggestions?
When using ItemProperty commands it interprets * as a wildcard. It's not freezing, it's searching every subkey of HKCR for "shell\Open With..." etc.
To force it to interpret the whole thing as a string path you need to use the -LiteralPath switch:
New-Item -Type String "HKCR:\*\shell\Open With Notepad++"
New-Item -Type String "HKCR:\*\shell\Open With Notepad++\command"
Set-ItemProperty -LiteralPath "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)" -value "C:\\Notepad++\\notepad++.exe %1"

Copy-Item works for IP address, not Computer Name

The following code will copy files to remote_computer if I use its IP address 10.10.10.10
$j = "remote_computer"
New-PSDrive -Name Z -PSProvider FileSystem -Root \\$j\share -Credential $credentials -ErrorAction Stop
Copy-Item -Path D:\ps_*able.ps1 -Destination \\10.10.10.10\share
Remove-PSDrive -name Z
This script will NOT copy over files if I use Z, the psdrive
$j = "remote_computer"
New-PSDrive -Name Z -PSProvider FileSystem -Root \\$j\share -Credential $credentials -ErrorAction Stop
Copy-Item -Path D:\ps_*able.ps1 -Destination Z
Remove-PSDrive -name Z
How to fix?
"Z" is not a valid path
Copy-Item -Path D:\ps_*able.ps1 -Destination Z:\