Having trouble with PowerShell recurse in VB6 folder - powershell

I am using a script to build VB6 files. Until now every folder was hard coded which is not the best solution, so I try to change the script to find and build .vbp file from the folder. Unfortunately it isn't even executed. The code is below. $outputfile is the logs file.
Get-ChildItem $folder -Include *.vbp -Recurse | foreach ($_) {
& vb6.exe /out $outputFile $_.FullName /make
}

My guess would be issues with the parsing the $_.fullname without enclosing in $(). Try this:
Get-ChildItem $folder -Include *.vbp -Recurse | foreach {
& vb6.exe /out $outputFile $($_.FullName) /make
}

Related

Advice with powershell script syntax

I am writing a powershell script to perform the following:
Within a folder Folder > Subfolder1 > Subfolder2 there are 30+ subfolders.zipin which there is another subfolder with 200 HRML files.
I would like to search for a keyword WTSE in the HTML files and any files containing such keyword would be moved to another folder.
My script looks as follows at the moment:
Get-childitem C:\Users\XXXXX\Desktop\Folder\ -filter *.html -recurse | select-string 'WTSE'|foreach-object -process{move-item} C:\Users\XXXXX\Desktop\Folder2`
You're almost there. The problem is with the part after ForEach-Object.
Since you are not searching for a string using regex, I would suggest adding the -SimpleMatch to the Select-String cmdlet.
Try below:
$sourceFolder = 'C:\Users\XXXXX\Desktop\Folder'
$destination = 'C:\Users\XXXXX\Desktop\Folder2'
(Get-ChildItem -Path $sourceFolder -Filter '*.html' -Recurse | Select-String -Pattern 'WTSE' -SimpleMatch) |
Move-Item -Destination $destination
The Move-Item cmdlet can take an array of paths and these can also accepts pipeline input, so there is no need to use ForEach-Object here.
Note I'm using brackets around the first part (Get-ChildItem ... -SimpleMatch). This prevents the error that the process cannot open the file because it is in use

powershell copy all folder structure and exclude one or more folders

I'm trying to use PowerShell to copy a folder with sub-folders from our users to a small backup. These folders contain a folder called "windows" I don't want to copy.
I have tried "exclude" but can't seem to get it to work.
This is the script so far:
Copy-Item "E:\Curos folder" -Exclude 'Windows' -Destination "E:\Curos folder backup" -Recurse -Verbose
I have read other posts but don't quiet understand how it works
It's my first time working with PowerShell
You are complete right.
Actually the script it's simpler than the one I have wrote before.
Here we go:
$source = "C:\Users\gaston.gonzalez\Documents\02_Scripts"
$destination = "D:\To Delete"
$exclude = "Windows"
$folders = Get-ChildItem -Path $source | Where {($_.PSIsContainer) -and ($exclude -notcontains $_.Name)}
foreach ($f in $folders){
Write-Host "This folders will be copied: $f"
Copy-Item -Path $source\$f -Destination $destination\$f -Recurse -Force
}
I'd use something like this:
Get-ChildItem $root -Directory -Recurse | % {$_.name -ne 'Windows'} | foreach {Copy-Item "$($_.FullName)" -Destination $dest -Recurse}
I haven't tested it but it's the skeleton of something you should be able to make work, although I don't find the point of using recurse on both, Get-ChildItem and Copy-Item my advice is to use it on Get-ChildItem.

zip files using powershell

This is what I am trying to do with powershell for zipping files.
Sort out files older than xxx days
Add those files in a zip file.
Delete the source files.
I have Powershell_Community_Extensions installed so I use Write-zip to do the job.
$Source = "\\network\share"
$files = Get-ChildItem -Path $Source | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-62)}
$files | Write-Zip -OutputPath $Source\Archive.zip -EntryPathRoot $Source -Append -Quiet
Remove-Item $files -Force
Issues:
I have to use -EntryPathRoot with Write-zip, otherwise it wont pick up network share
Remove-item also wont pickup files from network share, it says "Remove-Item : Cannot find path 'C:\Windows\system32\feb03.txt' because it does not exist.", why it deleting file from C:\Windows\system32\ instead of \\network\share
Write-zip -append did add files into the zip file, but it did not just add files in the root of that zip file, it actually created entire folder structure in the root of the zip and added newer filtered files in the end of that folder structure. I just want to add the newer filtered files into root of that zip file.
Any idea please?
Utilizing the v5 *Archive cmdlets:
$Source = '\\network\share'
$Files = Get-ChildItem -Path $Source |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-62) }
Compress-Archive -Path $Files.FullName -DestinationPath $Source\Archive.zip -CompressionLevel Optimal -Update
$Files | Remove-Item -Force

powershell script to archive files in many subfolders

i am trying to write down a script which can help me to archive multiple files in multiple subdirs. Also i need to exclude specific files.
So far i got that few lines script
$files = Get-ChildItem -Recurse -path "D:\path\to\folder" -Exclude *i.jpeg |
Where-Object { $_.FullName -notmatch '\\excludedir($|\\)' }
foreach ($file in $files)
{
C:\Program Files\7-zip\7z.exe" a -t7z -mx=9 -ms=on $file
}
Basically its searches recursively all subfolders for .jpeg files and gives me the list of them excluding the ones that ends with 'i'.jpeg lets say 'photoi.jpeg'.
This is working, but i cannot make it to the next step as i need to run 7zip for all listed files.
Can someone help me out here.
Thanks in advance :)
Not sure if you are trying save one big zip lots of individual ones, however I do something like this:
Set-Alias sz "C:\Program Files\7-zip\7z.exe"
$files = Get-ChildItem -Recurse -path "D:\path\to\folder" -Exclude *i.jpeg |
Where-Object { $_.FullName -notmatch '\\excludedir($|\\)' }
foreach ($file in $files)
{
$output = sz a -t7z -mx=9 -ms=on "$File" 2>&1
}
You might have to modify the zipping line has I have tested it using your command line options. Another nice touch is that I have captured the output of the command for reporting purposes.
You need to pass filenames, not objects, to 7z command.
#listfile syntax can help:
Get-ChildItem ...| Where-Object ...| select -expand fullname| out-file alist.txt -encoding utf8
'C:\Program Files\7-zip\7z.exe' a archive.7z -mx=9 -ms=on `#alist.txt
Remove-item .\alist.txt
note the backtick before #

Need a script to publish build output to a staging server

I am trying to write a PowerShell script that will copy a subset of files from a source folder and place them into a target folder. I've been playing with "copy-item" and "remove-item" for half a day and cannot get the desired or consistent results.
For example, when I run the following cmdlet multiple times, the files end up in different locations?!?!:
copy-item -Path $sourcePath -Destination $destinationPath -Include *.dll -Container -Force -Recurse
I've been trying every combination of options and commands I can think of but can't find the right solution. Since I'm sure that I'm not doing anything atypical, I'm hoping someone can ease my pain and provide me with the proper syntax to use.
The source folder will contain a large number of files with various extensions. For example, all of the following are possible:
.dll
.dll.config
.exe
.exe.config
.lastcodeanalysisissucceeded
.pdb
.Test.dll
.vshost.exe
.xml
and so on
The script needs to only copy .exe, .dll and .exe.config files excluding any .test.dll and .vshost.exe files. I also need the script to create the target folders if they don't already exist.
Any help getting me going is appreciated.
try:
$source = "C:\a\*"
$dest = "C:\b"
dir $source -include *.exe,*.dll,*.exe.config -exclude *.test.dll,*.vshost.exe -Recurse |
% {
$sp = $_.fullName.replace($sourcePath.replace('\*',''), $destPath)
if (!(Test-Path -path (split-path $sp)))
{
New-Item (split-path $sp) -Type Directory
}
copy-item $_.fullname $sp -force
}
As long as the files are in one directory, the following should work fine. It might be a bit more verbose than needed, but it should be a good starting point.
$sourcePath = "c:\sourcePath"
$destPath = "c:\destPath"
$items = Get-ChildItem $sourcePath | Where-Object {($_.FullName -like "*.exe") -or ($_.FullName -like "*.exe.config") -or ($_.FullName -like "*.dll")}
$items | % {
Copy-Item $_.Fullname ($_.FullName.Replace($sourcePath,$destPath))
}