Files
order-notification-automation/scripts/smb-native.ps1
T

34 lines
1.7 KiB
PowerShell

$ErrorActionPreference = 'Stop'
[Console]::InputEncoding = New-Object System.Text.UTF8Encoding($false)
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
try {
$orderSmbRequest = [Console]::In.ReadToEnd() | ConvertFrom-Json
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
public static class OrderSmbNative {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct NETRESOURCE {
public int scope, type, displayType, usage;
public string localName, remoteName, comment, provider;
}
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
public static extern int WNetAddConnection2W(ref NETRESOURCE resource, string password, string username, int flags);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool MoveFileExW(string existingName, string newName, int flags);
}
'@
if ($orderSmbRequest.operation -eq 'connect') {
$orderSmbResource = New-Object OrderSmbNative+NETRESOURCE
$orderSmbResource.type = 1
$orderSmbResource.remoteName = $orderSmbRequest.remote
$orderSmbCode = [OrderSmbNative]::WNetAddConnection2W([ref]$orderSmbResource, $orderSmbRequest.password, $orderSmbRequest.username, 4)
} elseif ($orderSmbRequest.operation -eq 'publish') {
$orderSmbMoved = [OrderSmbNative]::MoveFileExW($orderSmbRequest.source, $orderSmbRequest.target, 8)
$orderSmbCode = if ($orderSmbMoved) { 0 } else { [Runtime.InteropServices.Marshal]::GetLastWin32Error() }
} else { throw 'Unsupported operation' }
@{ ok = ($orderSmbCode -eq 0); code = $orderSmbCode } | ConvertTo-Json -Compress
} catch {
@{ ok = $false; code = -1 } | ConvertTo-Json -Compress
}