PowerShell to move file type up one dir - windows

I'm looking for a script the moves a certain file type *.msg up one directory but still remaining their project ID folder.
The current file structure looks like:
Host Folder\C12345678\A\test.msg
Host Folder\C12345678\B\test.msg
Host Folder\C99999999\F\test.msg
Host Folder\C56351114\T\test.msg
Host Folder\C69365814\I\test.msg
I want to remove the lettering, so just the project ID hosts the *.msg:
Host Folder\C12345678\test(A).msg
Host Folder\C12345678\test(B).msg
Host Folder\C99999999\test.msg
Host Folder\C56351114\test.msg
Host Folder\C69365814\test.msg
Currently, my code is just moving the various *.msg file to the Host folder, unable to duplicate folders.
$src = "..\Host Folder"
$files = Get-ChildItem $src -file -recurse
foreach ($file in $files) {
#Move-Item $file.PSPath $src
}
I mostly batch file so I'm not so good with PS but I need to add $src\$files?

Related

Push certificate to multiple windows servers

Can you please help me where to add credentials (all windows servers have same credentials) in this script to puch scripts
Point the script to a text file with a list of computers
$Computers = "C:\File Copy\Source Server\ComputerList.txt"
Sets the variable for the source file location
$Source = "C:\File Copy\prod.csv"
Sets the variable for the file destination
$Destination = "File copy\Servers"
Get the content of $computers and copy Source to Destination
Get-Content $Computers | ForEach-Object {Copy-Item $Source -Destination (Join-Path "\\$_\c`$\" $Destination)
Apologies in advance i am not an expert on powershell so dont know much can you explain a bit

Update files on FTP server folder hierarchy with local files from a single folder

I have a little-big problem. I need to copy/overwrite JPG files from my local FOLDER to server FOLDERS.
Is there a way to search and match JPG files on SERVER with my files on LOCAL and overwrite them in server folders? I do it manually and it takes lot of time.
There are 50 000 JPGs on server and I need to overwrite 20 000 of them in short time.
Many thanks for answers!!
There's no magic way to do your very specific task. You have to script it.
If you are on Windows, it's rather trivial to write a PowerShell script for this, using WinSCP .NET assembly and its Session.EnumerateRemoteFiles method:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property #{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.example.com"
UserName = "username"
Password = "password"
}
$remotePath = "/remote/path";
$localPath = "C:\local\Path";
# Connect
Write-Host "Connecting..."
$session = New-Object WinSCP.Session
$session.SessionLogPath = "upload.log"
$session.Open($sessionOptions)
# Enumerate remote files
$fileInfos =
$session.EnumerateRemoteFiles(
$remotePath, "*.*", [WinSCP.EnumerationOptions]::AllDirectories)
# And look for a matching local file for each of them
foreach ($fileInfo in $fileInfos)
{
$localFilePath = (Join-Path $localPath $fileInfo.Name)
if (Test-Path $localFilePath)
{
Write-Host ("Found local file $localFilePath matching remote file " +
"$($fileInfo.FullName), overwriting..."
# Command-out this line with # for a dry-run
$session.PutFiles($localFilePath, $fileInfo.FullName).Check()
}
else
{
Write-Host ("Found no local file matching remote file " +
"$($fileInfo.FullName), skipping..."
}
}
Write-Host "Done"
Save the script to a file (SortOutFiles.ps1), extract a contents of WinSCP .NET assembly package along with the script, and run it like:
C:\myscript>powershell -ExecutionPolicy Bypass -File SortOutFiles.ps1
Connecting...
Found local file C:\local\path\aaa.txt matching remote file /remote/path/1/aaa.txt, overwritting...
Found local file C:\local\path\bbb.txt matching remote file /remote/path/2/bbb.txt, overwritting...
Found local file C:\local\path\ccc.txt matching remote file /remote/path/ccc.txt, overwritting...
Done
You can first dry-run the script by commenting out the line with $session.PutFiles call.
(I'm the author of WinSCP)
download "Filezilla"... Upload your local files (all 50000 images).. If a image is already there in server,, it will ask you options.. select 'overwrite' and use 'apply for all'...

How to get the Dropbox folder in Powershell in Windows

Same question exists for Python here: How can I get the Dropbox folder location programmatically in Python?, or here for OSX: How to get the location of currently logined Dropbox folder
Same thing in Powershell. I need the path of DropBox to copy files to it (building a software and then copying it to dropbox to share with team).
This Dropbox help page tells us where this info is stored, ie, in a json file in the AppData of the user: https://www.dropbox.com/help/4584
function GetDropBoxPathFromInfoJson
{
$DropboxPath = Get-Content "$ENV:LOCALAPPDATA\Dropbox\info.json" -ErrorAction Stop | ConvertFrom-Json | % 'personal' | % 'path'
return $DropboxPath
}
The line above is taken from: https://www.powershellgallery.com/packages/Spizzi.Profile/1.0.0/Content/Functions%5CProfile%5CInstall-ProfileEnvironment.ps1
Note that it doesn't check if you've got a Dropbox business account, or if you have both. It just uses the personal one.
You can then use this base Dropbox folder to build your final path, for example:
$targetPath = Join-Path -Path (GetDropBoxPathFromInfoJson) -ChildPath 'RootDropboxFolder\Subfolder1\Subfolder2'
if (-not (Test-Path -Path $targetPath)) { throw "Path '$targetPath' not found!" }
--
Alternative way is using the host.db file, as shown on this page:
http://bradinscoe.tumblr.com/post/75819881755/get-dropbox-path-in-powershell
$base64path = gc $env:appdata\Dropbox\host.db | select -index 1 # -index 1 is the 2nd line in the file
$dropboxPath = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($base64path)) # convert from base64 to ascii

I want to fetch the name of the latest updated folder at particular path of FTP server

Using this command I am able to get the latest updated folder in Unix
ls -t1 | head -1
But how can I get the same in FTP server from Windows?
I want to get the name of the latest updated folder at particular path of FTP server. Could any one please help?
There's no easy way to do this with Windows shell commands.
You can:
Use ftp.exe to execute ls /path c:\local\path\listing.txt to save a directory listing to a text file.
Exit ftp.exe.
Parse the listing and find the latest files. Not an easy task for Windows shell commands.
It would be a way easier with a PowerShell script.
You can use FtpWebRequest class. Though it does not have an easy way to retrieve structured directory listing either. It offers only ListDirectoryDetails and GetDateTimestamp methods.
See Retrieving creation date of file (FTP).
Or use a 3rd-party library for the task.
For example with WinSCP .NET assembly you can do:
param (
$sessionUrl = "ftp://user:mypassword#example.com/",
$remotePath = "/path"
)
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl($sessionUrl)
# Connect
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
# Get list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)
# Select the most recent file
$latest =
$directoryInfo.Files |
Where-Object { -Not $_.IsDirectory } |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
}
else
{
Write-Host "The latest file is $latest"
}
See full example Downloading the most recent file.
(I'm the author of WinSCP)

powershell manage multiple windows service on multiple remote servers

I want to start/stop set of windows service on each of more than one remote servers. I've created one powershell in which i am passing values in a csv file for server name and service. but i want this should be in below manner:-
One text file shd contain list of remote server
One text file shd contain list of services.
One powershell script which will either start or stop all the services mentioned in second text file on first server listed in first text file, once done with first server it shd go to second server with all these services and so on till last entry in list of remote server.
I have found solution to above.
I've created two separate text files, one for server name say servers.txt and one is for service name say services.txt
In my PowerShell script I've written below and worked fine for me:
$serverList = gc servers.txt
$serviceList = gc services.txt
ForEach ($server in $serverList)
{
ForEach ($service in $serviceList)
{
Get-Service -Name $service -ComputerName $server | Start-service
}
}
Thanks again to all who have responded here on my question.
You could put the service names as a comma-separated list in a tab-separated file:
Hostname Services
HostA ServiceA,ServiceB
HostB ServiceA,ServiceC,ServiceD
HostC ServiceB,ServiceC
and process the file like this:
Import-Csv C:\path\to\list.tsv -Delimiter "`t" | % {
$services = $_.Services -split ','
Start-Service -Computer $_.Hostname -Name $services
}

Resources