copy files to multiple computers using powershell - powershell-2.0

I need to copy files to multiple computers from my computer with following specifications.
I need to provide username
I need to provide password also
while running it should not prompt again for password important
I used the following code but it asks for the password multiple times.
read-host -assecurestring | convertfrom-securestring | out-file e:\SSS\pass.txt
$password=get-content e:\SSS\pass.txt | convertto-securestring
$credential=new-object -typename System.Management.Automation.PSCredential -argumentlist KS\KS012\Administrator, $password

Script below should prompt for credentials and then prompt to ask for location of txt file which has line seperated list of PC's you want to deploy to. Then it will ask for the location of the file you want to copy and then ask for the destination with a preformated UNC c$ formatting.
You can adjust this to set the list of PCs, location of file to copy and desitnation to hardcoded.
Get-Credential domain\usermname
$PC = Read-Host "Location of PC List"
$FileLocation = Read-Host "Enter File Location"
$FileDestination = Read-Host "Enter File Destination"
Get-Content $PC | foreach {Copy-Item $FileLocation -Destination \\$_\c$\$FileDestination}
:)

Related

Bulk Export file paths from sharepoint

I am adding 200+ hyperlinks to an excel file for easier access to sharepoint folders. Rather than manually pulling each one I am trying to bulk export the file paths from a sharepoint folder. Any ideas on how to do this?
We can use PnP PowerShell to achieve it.
#Config Variables
$SiteURL = "https://tenant.sharepoint.com/sites/lz"
$ListName = "DL0906"
$FolderRelativeURL= "/sites/lz/DL0910/Test"
$ExportFile="C:\temp\FileUrls.csv"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Get All Items from the Folder
$CAMLQuery = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileDirRef'/><Value Type='Text'>$FolderRelativeURL</Value></Eq></Where></Query></View>"
$FolderItems = Get-PnPListItem -List $ListName -Query $CAMLQuery
$FileCollection = #()
ForEach($Item in $FolderItems)
{
$ExportFileUrl = New-Object PSObject
$ExportFileUrl | Add-Member -MemberType NoteProperty -name "File URL" -value $Item["FileRef"]
$FileCollection += $ExportFileUrl
}
$FileCollection | Export-CSV $ExportFile -NoTypeInformation
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Refer to: SharePoint Online: Get All Files from a Folder using PowerShell

network share script - auto check drive letter and path

I have this script that adds a path based on the username:
$credential = Get-Credential
$user=$credential.GetNetworkCredential().UserName
New-PSDrive -Name x -PSProvider FileSystem -Root "\\192.168.1.1\$($user)" -Credential $credential -persist
I'm trying to make it more robust, how can I edit it to check which drive letter is available and if the network path is already mapped with an existing drive letter, so no error will be made if the end user runs it multiple times.
Free drive letters:
ls function:[d-z]: -n | ?{ !(test-path $_) }
Credit goes to network share script - auto check drive letter and path

How to rename computer after join the domain in PowerShell v2.0

I am using PowerShell 2.0 and using the following code to join domain:
$domain = "test.org.hk"
$password = "password" | ConvertTo-SecureString -AsPlainText -Force
$username = "$domain\Administrator"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential
After joining the domain, I use the following code to rename computer:
(Get-WmiObject Win32_ComputerSystem).Rename($nameStr)
I find that I cannot change computer name.
But, if I don't join the domain, I can use this code to change computer name.
Therefore, how to rename computer after join the domain in PowerShell v2.0?

PowerShell - Problems Passing Variables into ForEach Loop

I am trying to enumerate a list of servers from Active Directory, and then insert the server name into a UNC path as part of a copy command.
When I execute the script, I get the result below. I think that maybe I have to convert my variable, but I am not sure what to convert it to.
VERBOSE: Performing the operation "Copy File" on target "Item: C:\davidtemp\Logo.png Destination: \#{name=NCIDITSTWEB07}\c$\program files...
$webdev = Get-ADOrganizationalUnit -filter {name -like "*dev*"} | where {$_.DistinguishedName -like "*relativity*"}
$ServerList = Get-ADComputer -SearchBase $webdev | where {$_.name -like "*web*"} | select name | sort name
Foreach($server in $ServerList)
{
$scriptBlockwork = { copy C:\davidtemp\Logo.png "\\$server\c$\program files\web\images" -Force -Verbose}
Invoke-Command -ScriptBlock $scriptBlockwork -verbose
}
I reached out to a friend who was able to help. I was not defining the variable properly.
I needed to use -expandProperty to get the results into a format that worked with the pipeline
$ServerList = Get-ADComputer -SearchBase $webdev | where {$_.name -like "web"} | select -expandProperty name
Hopefully this helps someone else who might be having a similar issue.

Search in/for text in files

I am currently learning LPTHW Ex 46. In his video tutorial, Zed had done the following commands:
Find NAME within files using grep -r "NAME" *.
Find all files with extension ending in .pyc using find . -name "*pyc" -print.
Unfortunately, the above code does not work on Windows PowerShell. May I know what their Windows PowerShell equivalents are?
Based on my search, item 1 can be replaced by Select-String. However, it is not as good as we can only search specific files and not directories. For example, while this would work:
Select-String C:\Users\KMF\Exercises\Projects\gesso\gesso\acrylic.py -pattern "NAME"
this would not:
Select-String C:\Users\KMF\Exercises\Projects\gesso -Pattern "NAME"
and it gives the following error
Select-String : The file C:\Users\KMF\Exercises\Projects\gesso can not be read: Access to the path 'C:\Users\KMF\Exercises\Projects\gesso' is denied.
For item 2 I could not find a similar function.
grep and find are Unix/Linux shell commands. They won't work in PowerShell unless you install a Windows port of them.
As you already found out, Select-String is the PowerShell equivalent for grep. It doesn't recurse by itself, though, so you have to combine it with Get-ChildItem to emulate grep -r:
Get-ChildItem -Recurse | Select-String -Pattern 'NAME'
For emulating find you'd combine Get-ChildItem with a Where-Object filter:
Get-ChildItem -Recurse | Where-Object { $_.Extension -eq '.pyc' }
PowerShell cmdlets can be aliased to help administrators avoid extensive typing (since PowerShell statements tend to be rather verbose). There are several built-in aliases, e.g. ls or dir for Get-ChildItem, and ? or where for Where-Object. You can also define aliases of your own, e.g. New-Alias -Name grep -Value Select-String. Parameter names can be shortened as long as the truncated parameter name remains unique for the cmdlet. When cmdlets allow positional parameters they can even be omitted entirely.
With all of the above your two PowerShell statements can be reduced to the following:
ls -r | grep 'NAME'
ls -r | ? { $_.Extension -eq '.pyc' }
Note however, that aliases and abbreviations are mainly intended as an enhancement for console use. For PowerShell scripts you should always use the full form, not only for readability, but also because aliases may differ from environment to environment. You don't want your scripts to break just because they're run by someone else.

Resources