Hyper-V
5 TopicsPowerShell Direct - Unable to open Excel workbook with Excel.Application ComObject
Hello All, Hoping someone may be able to enlighten me or provide an alternative. Running in a local PS session on a Win10 client with Office 2019 installed, I am able to run these commands, open, and use content from an xlxs file: $file = "C:\TestFiles\Test.xlsx" $excel = New-Object -ComObject Excel.Application $workbook = $excel.Workbooks.Open($file) <...more code here but not relevant to the conversation....> When I try to use the same command using PowerShell Direct on the host where the VM is running, I get this: <Enter-PSSession -VMName WIN10.....command already run and connected. User account connecting is a local administrator in the Workgroup joined VM...> [WIN10]: PS C:\Users\TestUser\Documents> $file = "C:\TestFiles\Test.xlsx" [WIN10]: PS C:\Users\TestUser\Documents> $excel = New-Object -ComObject Excel.Application [WIN10]: PS C:\Users\TestUser\Documents> $workbook = $excel.Workbooks.Open($file) Microsoft Excel cannot access the file 'C:\TestFiles\Test.xlsx'. There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook. At line:1 char:1 + $workbook = $excel.Workbooks.Open($file) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException Yes, the file is there. No it isn't opened by another program. Not trying to save. Again, the commands work over and over when run in a local PS session. I'm leaning toward using the ImportExcel PS module but I'm not sure it will allow me to find formulas in a spreadsheet (I've not seen where that can be done). Regardless, I've repro'd this on three different Hyper-V servers and it seems like there is a problem with ComObject. I'm not versed well enough to know much more than that. I've read about DCOM Config and allowing permissions to Excel but there are no entries for either Excel or a CLSID for Excel. This is simple for anyone to repro. Just have a Win10 VM with Office and create an Excel file. You don't need to do anything with the file. This all falls apart in PS Direct when the workbook is being opened. Thank you in advance for any help. TomSolved19KViews0likes3CommentsBlog Post: Reconfigure Hyper-V External switch to the correct network adapter
Wrote a little blog post about how to change the External switch to the correct network adapter, convenient if you use Wi-Fi at the office and a wired connection at home for example. More about that here: https://powershellisfun.com/2022/10/08/reconfigure-hyper-v-external-switch-to-the-correct-network-adapter/ The script: function Set-CorrectHyperVExternalSwitchAdapter { param ( [parameter(Mandatory = $true)][string]$SwitchName ) #Validate SwitchName if (-not (Get-VMSwitch -Name $SwitchName | Where-Object { $_.SwitchType -eq 'External' -and $_.AllowManagementOS -eq $True })) { Write-Warning ("External Hyper-V Switch {0} can't be found or has no 'Allow management operating system to share this network adapter' enabled, exiting..." -f $SwitchName) return } #retrieve external switch(es) with Allow Management OS on and get Network adapter with Up state $externalswitch = Get-VMSwitch | Where-Object { $_.Name -eq $SwitchName -and $_.SwitchType -eq 'External' -and $_.AllowManagementOS -eq $True } $connectedadapter = Get-NetAdapter | Where-Object Status -eq Up | Sort-Object ifIndex | Select-Object -First 1 #Set VMSwitch(es) properties so that the connected adapter is configured try { Set-VMSwitch $externalswitch.Name -NetAdapterName $connectedadapter.Name -ErrorAction Stop Write-Host ("Reconfiguring External Hyper-V Switch {0} to use Network Adapter {1}" -f $SwitchName, $connectedadapter.Name) -ForegroundColor Green } catch { Write-Warning ("Failed reconfiguring External Hyper-V Switch {0} to use Network Adapter {1}" -f $SwitchName, $connectedadapter.Name) } }731Views0likes0CommentsBlogpost - Creating a Hyper-V VM running Evaluation versions of Windows server using PowerShell
Wrote a blogpost about how you can deploy Hyper-V VM's on your machine using PowerShell, below is the script which I made #Requires -RunAsAdministrator #Start a stopwatch to measure the deployment time $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() #Detect if Hyper-V is installed if ((Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State -ne 'Enabled') { Write-Warning ("Hyper-V Role and/or required PowerShell module is not installed, please install before running this script...") return } else { Write-Host ("Hyper-V Role is installed, continuing...") -ForegroundColor Green } #Retrieve all Server Operating System VHD links from the Microsoft Evaluation Center $totalcount = $null $urls = @( 'https://www.microsoft.com/en-us/evalcenter/download-windows-server-2012-r2', 'https://www.microsoft.com/en-us/evalcenter/download-windows-server-2016', 'https://www.microsoft.com/en-us/evalcenter/download-windows-server-2019', 'https://www.microsoft.com/en-us/evalcenter/download-windows-server-2022' ) #Loop through the urls, search for VHD download links and add to totalfound array and display number of downloads $ProgressPreference = "SilentlyContinue" $totalfound = foreach ($url in $urls) { try { $content = Invoke-WebRequest -Uri $url -ErrorAction Stop $downloadlinks = $content.links | Where-Object { ` $_.'aria-label' -match 'Download' ` -and $_.'aria-label' -match 'VHD' } $count = $DownloadLinks.href.Count $totalcount += $count Write-host ("Processing {0}, Found {1} Download(s)..." -f $url, $count) -ForegroundColor Green foreach ($DownloadLink in $DownloadLinks) { [PSCustomObject]@{ Title = $content.ParsedHtml.title.Split('|')[0] Name = $DownloadLink.'aria-label'.Replace('Download ', '') Tag = $DownloadLink.'data-bi-tags'.Split('"')[3].split('-')[0] Format = $DownloadLink.'data-bi-tags'.Split('-')[1].ToUpper() Link = $DownloadLink.href } } } catch { Write-Warning ("{0} is not accessible" -f $url) return } } #Select VHD from the list $VHD = $totalfound | Out-GridView -OutputMode Single -Title 'Please select the VHD file to use and click OK' | Select-Object Name, Link if (($VHD.Name).Count -ne '1') { Write-Warning ("No VHD file selected, script aborted...") return } #Set VM Parameters $VMname = Read-Host 'Please enter the name of the VM to be created, for example W2K22SRV' if ((Get-VM -Name $VMname -ErrorAction SilentlyContinue).count -ge 1) { Write-Warning ("{0} already exists on this system, aborting..." -f $VMname) return } $VMCores = Read-Host 'Please enter the amount of cores, for example 2' [int64]$VMRAM = 1GB * (read-host "Enter Maximum Memory in Gb's, for example 4") $VMdir = (get-vmhost).VirtualMachinePath + $VMname $SwitchName = Get-VMSwitch | Out-GridView -OutputMode Single -Title 'Please select the VM Switch and click OK' | Select-Object Name if (($SwitchName.Name).Count -ne '1') { Write-Warning ("No Virtual Switch selected, script aborted...") return } #Create VM directory try { New-Item -ItemType Directory -Path $VMdir -Force:$true -ErrorAction SilentlyContinue | Out-Null } catch { Write-Warning ("Couldn't create {0} folder, please check VM Name for illegal characters or permissions on folder..." -f $VMdir) return } finally { if (test-path -Path $VMdir -ErrorAction SilentlyContinue) { Write-Host ("Using {0} as Virtual Machine location..." -f $VMdir) -ForegroundColor Green } } #Download VHD file to the VirtualMachinePath\VMname write-host ("Downloading {0} to {1}..." -f $vhd.Name, $VMdir) -ForegroundColor Green $VHDFile = "$($VMdir)\$($VMname)" + ".vhd" $VMPath = (Get-VMHost).VirtualMachinePath + '\' Invoke-WebRequest -Uri $vhd.Link -OutFile $VHDFile #Create VM with the specified values try { New-VM -Name $VMname -SwitchName $SwitchName.Name -Path $VMPath -Generation 1 -NoVHD:$true -Confirm:$false -ErrorAction Stop | Out-Null } catch { Write-Warning ("Error creating {0}, please check logs and make sure {1} doesn't already exist..." -f $VMname, $VMname) } finally { if (Get-VM -Name $VMname -ErrorAction SilentlyContinue | Out-Null) { write-host ("Created {0}..." -f $VMname) -ForegroundColor Green } } #Configure settings on the VM, CPU/Memory/Disk/BootOrder try { Write-Host ("Configuring settings on {0}..." -f $VMname) -ForegroundColor Green Set-VM -name $VMname -ProcessorCount $VMCores -DynamicMemory -MemoryMinimumBytes 64MB -MemoryMaximumBytes $VMRAM -MemoryStartupBytes 512MB -ErrorAction SilentlyContinue | Out-Null Add-VMHardDiskDrive -VMName $VMname -Path $VHDFile -ControllerType IDE -ErrorAction SilentlyContinue | Out-Null } catch { Write-Warning ("Error setting VM parameters, check {0} settings..." -f $VMname) return } #The end, stop stopwatch and display the time that it took to deploy $stopwatch.Stop() Write-Host ("Done, the deployment took {0} hours, {1} minutes and {2} seconds" -f $stopwatch.Elapsed.Hours, $stopwatch.Elapsed.Minutes, $stopwatch.Elapsed.Seconds) -ForegroundColor Green For more information about it: https://powershellisfun.com/2022/06/01/creating-a-hyper-v-vm-running-evaluation-versions-of-windows-server-using-powershell/1.1KViews1like0CommentsWhat is difference between File size and Size in the output of GET-VHD command?
In resizing VHD using Resize-VHD command, the property SizeinBytes is accepting value greater than the "Size" property in the output of the GET-VHD command. But my actual size of VHD is in "FileSize" property. I am unable to figure out the relation between File size and Size. I would be glad to know the relation between the FileSize and Size properties or the unit of Size property (Kb/Mb/any other related). Thank you in advance3.2KViews0likes1CommentImp: Unable to remove HyperV in windows 10 using Powershell Script - Urgent
Hi All , I am unable to remove Hyper V from my client machine using below script . For your information , I am running the below script using elevated priviledge.. Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All Getting the error message " Referenced Assembly could not be found " .. I have tried using DISM method of removing using CMD prompt with elevated priviledge , it didn't work.. and also updated the update file . It is not all working and I am not finding any solution in Microsoft Website . Need all expert solution to fix this error , otherwise I have to reload the OS. Regards, Manas Saha1.9KViews0likes3Comments