Using Powershell to make USB Bootable Drive - powershell

I am trying to automate someone taking a ISO and making that into a bootable USB. I need help with that last command in the script which is xcopy. How can I copy all the files from the user imputed $ISO to the USB drive X:? I am having issues using "$ISO" and ":" in a path. The last command needs work cause currently it errors out.
Write-Host "Please mount your ISO and insert your USB stick"
$ISO = Read-Host -Prompt 'Please enter your mounted ISO drive letter'
Write-Host "Your mounted drive letter is seleted as $ISO"
Get-Disk
$USBDisk = Read-Host -Prompt 'Please enter your USB Disk Number from above'
Get-Disk $USBDisk | Clear-Disk -RemoveData
New-Partition -DiskNumber $USBDisk -Size 30000 MB -IsActive -DriveLetter X | Format-Volume -FileSystem FAT32 -NewFileSystemLabel Win10
xcopy $ISO:\* X:\ /S

Try:
xcopy ${ISO}:\* X:\ /S
This is an alternate method of referencing a variable and is useful when you need a trailing colon and don't want to confuse the parser with looking for a variable namespace.

Related

Powershell Dynamic path location for files

I'm trying to make a script that installs apps etc. the script contains 7 ps1 scripts and they are linked together but when I move the folder the script won't work since the path changed is there a way so I can always have the right path?
& 'Z:\Windows installatie\Scripts\Menus\Apps.ps1'
this is when it's from a USB but the drive letter always changes.
I tried using a wild card but that didn't work.
& '*\Windows installatie\Scripts\Menus\Apps.ps1'
If you would like to test for the existence of a folder or file under an unknown drive letter, and you know the path is going to be unique enough, then you could just test for it by iterating through Get-PSDrive -PSProvider FileSystem.
$Drive = Get-PSDrive -PSProvider FileSystem | Where-Object { Test-Path ($_.Root + "path\to\myScript.ps1") }
if ($null -ne $Drive -and $Drive.Count -eq 1) {
& (Join-Path -Path $Drive.Root -ChildPath "path\to\myScript.ps1")
}
Not incredibly elegant, but will do the job.
If you know that you only have one USB mounted at a time, then you could also check for details about the drive that is a USB.

Take a disk offline from powershell

normally to take a disk offline I use the windows disk partition manager. now i would need to do it with a power shell using the drive letter.
I tried with the command:
Set-Partition -DriveLetter E -IsOffline $True
i get this error from powershell:
Set-Partition : The volume still has access path to it.
Any suggestions?
I solved it like this:
set-disk (get-partition -DriveLetter E | get-disk | select number -ExpandProperty number) -isOffline $true

Argument -MemoryStartupBytes via variable

Good evening everybody,
I need help with a simple variable in Powershell. I'm trying to auto the creation of VM in Hyper-V trough Powershell.
I'm trying to prompt via Read-Host the amount of RAM GB but I received the same error all time.
$ram = Read-Host -Prompt "Ram to use"
New-VM -Name $vm -MemoryStartupBytes $ram -BootDevice VHD -NewVHDPath C:\HyperV\Virtualmachines\$vm.vhdx -Path C:\HyperV\Virtualmachines -NewVHDSizeBytes $disc1 -Generation 2 -Switch Data
New-VHD -Path C:\HyperV\virtualmachines\"$vm"_2.vhdx -SizeBytes 40GB -Dynamic
This is the error I received.
Two of them, one from the bytes and the other the correct format
New-VHD : Cannot bind parameter 'SizeBytes'. Cannot convert value
"[60GB]" to type "System.UInt64". Error: "Input string was not in a
correct format."
The minimum amount of memory you can assign to this virtual machine is
'32' MB.
I dont understand why is not working with GB if a put myself the amount in the properly powershell work fine but if I write in the variable doesnt work
A user can type in anything in Read-Host..
To remove the fluff, do something like
$ram = Read-Host -Prompt "Ram to use (GB)"
$ram = [uint64]($ram -replace '\D') * 1GB # now you have the value as UInt64

Powershell: Initialise and mount VHD disks

Powershell: Initialise and mount VHD disks
Using Powershell and VBoxManage (VirtualBox), I can create and mount a VHD disk, as follows:
$vhdpath="c:\myvhd.vhd"
vboxmanage createmedium disk --filename $vhdpath --sizebyte 200 --format VHD --variant Fixed
$vhd = Mount-DiskImage -PassThru $vhdpath -StorageType VHD
if(-not $vhd) {
Write-Host "Error mounting VHD"
exit
}
$vhd=Get-DiskImage -ImagePath $vhd.ImagePath
Initialize-Disk -Number $vhd.Number -PartitionStyle MBR
$partition = New-Partition -AssignDriveLetter -UseMaximumSize -DiskNumber $vhd.Number
$volume = Format-Volume -FileSystem FAT32 -Confirm:$false -Force -Partition $partition
Of course it is possible to replace vboxmanage ... with Hyper-V equivalent commands.
Two questions:
When the VHD disk is visible to PS, so is to the OS and, before the PS script formats it, an annoying Windows pop-up appears proposing to format the disk. How can I get rid of it?
In general, it is possible to mount a VHD disk as a removable disk. Is it possible to do so with PS too?
I am primarily (but not necessarily) looking for commands that do not require Hyper-V being installed.

copy a directory from a remote system to a USB drive replace a hardware image and then copy the directory back sounds simple enough

Thought this would be simple but.. i'm not a programmer im just trying to make something more efficient than having to re-type command after command on 20 systems mistakes happen.
so what im trying to do is copy a directory from a remote system to a USB drive replace a hardware image and then copy the directory back sounds simple enough I think it may be a double hop issue but I am lacking in how to fix it I am using a Domain Admin acccount and running powershell as administrator.
Clear #Clear Screen
$USBDrive = read-host -prompt "Drive letter for USB Drive?" #Get Drive Letter for EXT Media
cd $USBDrive':' #Change to external media drive
$BGM = read-host -prompt "# of BGM's?" #QTY of BGM's
# Create copies of Convertor Files
DO{
Write-Host BGM# $BGM
Test-Path \\172.16.10.$BGM"\c$\Program Files\Box Tech\BoxManager Version 3.7\172.16.10."$BGM"_cvtrdata"
New-item \App0$BGM -itemType Directory
Copy-Item -path \\172.16.10.$BGM"\c$\Program Files\Box Tech\BoxManager Version 3.7\172.16.10."$BGM"_cvtrdata" -destination \\App0$BGM\ -Recurse
$BGM = $BGM - 1
}
WHILE($BGM -gt 0)
Test-Path returns that it directory does not exist, Test-Path \172.16.10.$BGM"\c$\Program Files\Box Tech\BoxManager Version 3.7\172.16.10."$BGM"_cvtrdata"
If I use the exact path it returns true in test-path Test-Path "\172.16.10.1\c$\Program Files\Box Tech\BoxManager Version 3.7\172.16.10.1_cvtrdata"
but the copy fails I wold even be happy using robocopy to copy the files.