Auto Mount .vhdx on Startup - windows

I have a Surface Pro 4 with a 128 GB micro SD card to expand storage space. Sadly, Windows sees the SD card as a removable device and not a permanent hard drive, I did find a guide on how to work around this(See link below), however I am having trouble automatically mounting a .vhdx on startup. I created a .vhdx on the SD card, as the guide said, and want the .vhdx to mount on startup using task scheduler.
I have tried to use powershell and a .ps1 file to mount the .vhdx, however, this has not worked.
Mount-DiskImage A:\Fix.vhdx
Screenshot: Disk Management
I am following this guide: Configure Surface to use MicroSD as Primary Storage

According to your screenshot, Fix.vhdx is on I:.
Mount-DiskImage I:\Fix.vhdx
Note that, depending on your OS (Windows 8), you must be an Administrator to mount the disk. If you have the Hyper-V Cmdlets available, prefer ´Mount-VHD´.

I found a solution better than using powershell and task scheduler. VHD Attach is a utility made by Medo64 and that automatically mounts VHDs on startup.

Here is a simple way to mount a .vhd or .vhdx and assign it a drive letter utilizing PowerShell:
$VolumesVHDX = Mount-VHD -Path C:\mynewdisk.vhdx -PassThru | Get-Disk | Get-Partition | Get-Volume | Select-Object DriveLetter
Set-Partition -DriveLetter $VolumesVHDX.DriveLetter -NewDriveLetter L -Confirm:$false -ErrorAction SilentlyContinue
Save it as a PowerShell script, then create a Scheduled Task to run at system startup.
Command > PowerShell.exe
Arguments > "C:\Mount_VHDX_Drives.ps1"

Related

Powershell copy file from domain server to workgroup shared folder

Current situation:
We've got a domain with mostly Win Server 2022, including one database server. Next to that our backup server is in a different (accessible) VLAN, but did not join the domain (still in Workgroup). I've got a shared folder on our backup-server with permissions to Everyone.
Future situation:
I would love to write a powershell-script that automatically sends file from our Database-server (in domain) to our backup server (workgroup).
I keep struggling with permissions and auomatically putting them into the PowerShell scripting ... Anyone has got a solution to this?
You can authorize against the share with net-use first, then call your powershell
net use \\server\share /user:<domain\username> <password>
Or if you want to go powershell only, use the New-PSDrive cmdlet.
New-PSDrive -Name P -PSProvider FileSystem -Root \\Server01\Public -Credential user\domain -Persist
Hope this helps.

Docker for windows: Bind mounting an Azure SMB drive fail

I spent days on this issue, can't figure out how it's done?
Mounting the drive letter from Azure file share works fine using
New-PSDrive -Name T -PSProvider FileSystem -Root "\\xxxx.file.core.windows.net\share" -Scope Global -Persist
The drive can be accessed in both PowerShell and in explorer, even after windows reboot.
But when trying to mount that in a container fail to say the path does not exist.
docker run -d --name webserver1 -v T:\:c:\share -p 80:80 microsoft/iis
Error response from daemon: invalid volume specification: 'T:\:c:\share': invalid mount config for type "bind": bind source path does not exist: t:\.
I also tried to add the SMB drive via
New-SmbGlobalMapping -RemotePath "\\xxxx.file.core.windows.net\share" -Credential $credentialObject -LocalPath T: -FullAccess #( "NT AUTHORITY\SYSTEM", "NT AUTHORITY\NetworkService" ) -Persistent $true -RequirePrivacy $true
Then I can bind mount T: but get "access was denied" instead when trying to check c:\share inside the container. What am I doing wrong? Also referring to this: https://github.com/moby/moby/issues/37863
If there is another way to mount the SMB share directly into the container without first mounting it in windows, that would be fine too. I solved it with Linux but doesn't seem to be possible in windows.

Running an installer from a windows share using ansible

I want to log into a Windows server using a local user, then map a network drive using an Active Directory user and run an installer from it automated with Ansible.
I followed the suggestion from this question to create a PowerShell script and do the mount and the install. I used that script as follows:
Inventory:
[winserver]
windows
[winserver:vars]
ansible_user="local_user"
ansible_password="P#ssw0rd"
ansible_connection="winrm"
ansible_winrm_cert_validation=ignore
win_user="domain\aduser"
win_pass="P#55w0rd"
task yaml:
---
- name: Mount and run a script
script: 'files/maprun.ps1 -map_user {{ win_user }} -map_password {{ win_pass }} -script z:\ascript.ps1'
And the maprun.ps1 script contains the following:
param(
$map_user,
$map_password,
$script
)
$PWord="$map_password"|ConvertTo-SecureString -AsPlainText -Force
$myCreds=New-Object System.Management.Automation.PsCredential($map_user,$PWord)
New-PSDrive -Name "Z" -PSProvider "FileSystem" -Root "\\domain\share" -Credential $myCreds
echo Invoke-Command -ScriptBlock $script
And I get the error:
New-PSDrive: A specified logon session does not exist. It may already have
been terminated
Most hits talk about a double-hop problem, but I am trying to specify different credentials in the remote script, so this isn't a double hop problem. The other answer suggests this ought to be possible. The script works in interactive mode, so it is something to do with being in batch mode. Any ideas how I can get this to work?
I am using Ansible 2.3.1.0 on Red Hat Enterprise Linux. Windows is Windows Server 2012 R2. Scripts were manually typed, sorry for any typos.
I can make a few suggestions.
As you've written it, the script "maprun.ps1" must be saved in the "files" directory of your Linux controller machine. If it is, the "maprun.ps1" script will be copied by Ansible to the remote host, executed and deleted. If "maprun.ps1" is going to call "ascript.ps1" you need to copy "ascript.ps1" over to the remote host before calling "maprun.ps1" and have it's location defined in the context of "maprun.ps1".
You don't need parameter names before the args in your playbook "script:" command.
Args internal to your Ansible playbook need double curly brackets and quotes around them.
As an alternative to connecting to the network drive with the New-PSDrive command you could try the "net use" command as shown below.
So instead of
script: 'files/maprun.ps1 -map_user {{ win_user }} -map_password {{ win_pass }} -script z:\ascript.ps1'
Try
script: files/maprun.ps1 "Z:" "{{ win_user }}" "{{ win_pass }}" "z:\ascript.ps1"
Where maprun.ps1 contains the following...
# connect to a shared resource and run script
param(
[string]$map_server,
[string]$map_user,
[string]$map_password,
[string]$script
)
# connect to the network drive
net use $map_server $map_password /USER:$map_user
Invoke-Item (start powershell ((Split-Path $MyInvocation.InvocationName) + $script))
net use $uncServer /delete

Accessing SMB share from Windows when using Powershell

I'm trying to build a custom VeeamZip backup script using PowerShell from a Windows 7 box. The box doesn't have the space requirements to hold the actual data itself, it's just the catalyst to manage the VeeamZip files.
I've been hunting around and found a solution here on SO to work around the fact that the PS-Drive command doesn't function using the -Credentials flag in PowerShell 2.0 using this snippet:
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("u:", "\\share\point", $false, "user", "pass")
I can verify this is properly mounting the share and is searchable using Powershell, but when using the VeeamZip Powershell commands, the path U:\ isn't available.
I tried then using the net use command in Powershell which also mounted the volume, but even with /persistent:yes it won't show in Explorer and the backups fail.
What can I do? There has to be option to get this to work. I'm a Linux guy so I'm not powershell wiz.
EDIT: I've now updated to Powershell 3.0 and I still can't get it work...
I'm trying the following snippet, but only Powershell has access the new drive:
$credential = Get-Credential
New-PSDrive -Name V -PSProvider FileSystem -Root \\server\share -Credential $credential -Persist
EDIT: The New-PSDrive function now works to mount the volume in Explorer and can be browsed as expected as long as I don't launch PowerShell as administrator. The dilemma is now that I can't use the VeeamZip tool because it requires Admin to function. Ideas welcome.

Install network printer on a remote computer

I am hoping someone can point me in the right direction.
I want to be able to sort of manage users' network printers remotely. Which means, either using something like PowerShell or C# to select the computer name or IP address, then choose the printer (which is on a print server so \PrintServer\Printer01) and have it install on the target user's machine.
I've seen something that did say because of permissions, I might have to fake the identity to properly have it installed for the user.
I noticed powershell has a Add-Printer cmdlet, but it tells me it's not recognized on my machine. But running something like: add-printer -r CompName -p \server\Printer01 would then have the remote computer CompName fetch the printer info from the print server and install it.
I'm not dreaming am I? lol
The Win32 functions AddPrinterDriver and AddPrinter can install printers on remote machines. However, you will have to copy all the necessary files for the driver to the remote machine's \windows\system32\spool\drivers\[w32x86|x64] directory prior to calling the AddPrinterDriver function.
These Powershell commands can help:
Add PrinterPorts
Add-PrinterPort -Name $PrinterPort -PrinterHostAddress $PrinterIP -ComputerName $Computer
Add Printers
Add-Printer -computername $Computer -name $PrinterName -PortName $PrinterName-DriverName "HP Universal Printing PCL 6"

Resources