找回密码  加入

AUTOIT CN

搜索
查看: 226|回复: 7

[AU3基础] powershell代码转AU3

[复制链接]
发表于 2023-3-2 23:20:04 | 显示全部楼层 |阅读模式
求教大佬

如何把下面的power shell代码转成AU3?谢谢
  1. @exit /b

  2. :embed:
  3. function UninstallLicenses($DllPath) {
  4.     $DynAssembly = New-Object System.Reflection.AssemblyName('Win32Lib')
  5.     $AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
  6.     $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('Win32Lib', $False)
  7.     $TypeBuilder = $ModuleBuilder.DefineType('sppc', 'Public, Class')
  8.     $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
  9.     $FieldArray = [Reflection.FieldInfo[]] @([Runtime.InteropServices.DllImportAttribute].GetField('EntryPoint'))

  10.     $Open = $TypeBuilder.DefineMethod('SLOpen', [Reflection.MethodAttributes] 'Public, Static', [int], @([IntPtr].MakeByRefType()))
  11.     $Open.SetCustomAttribute((New-Object Reflection.Emit.CustomAttributeBuilder(
  12.                 $DllImportConstructor,
  13.                 @($DllPath),
  14.                 $FieldArray,
  15.                 @('SLOpen'))))

  16.     $GetSLIDList = $TypeBuilder.DefineMethod('SLGetSLIDList', [Reflection.MethodAttributes] 'Public, Static', [int], @([IntPtr], [int], [guid].MakeByRefType(), [int], [int].MakeByRefType(), [IntPtr].MakeByRefType()))
  17.     $GetSLIDList.SetCustomAttribute((New-Object Reflection.Emit.CustomAttributeBuilder(
  18.                 $DllImportConstructor,
  19.                 @($DllPath),
  20.                 $FieldArray,
  21.                 @('SLGetSLIDList'))))

  22.     $UninstallLicense = $TypeBuilder.DefineMethod('SLUninstallLicense', [Reflection.MethodAttributes] 'Public, Static', [int], @([IntPtr], [IntPtr]))
  23.     $UninstallLicense.SetCustomAttribute((New-Object Reflection.Emit.CustomAttributeBuilder(
  24.                 $DllImportConstructor,
  25.                 @($DllPath),
  26.                 $FieldArray,
  27.                 @('SLUninstallLicense'))))

  28.     $SPPC = $TypeBuilder.CreateType()
  29.     $Handle = [IntPtr]::Zero
  30.     $SPPC::SLOpen([ref]$handle) | Out-Null
  31.     $pnReturnIds = 0
  32.     $ppReturnIds = [IntPtr]::Zero

  33.     if (!$SPPC::SLGetSLIDList($handle, 0, [ref][guid]"0ff1ce15-a989-479d-af46-f275c6370663", 6, [ref]$pnReturnIds, [ref]$ppReturnIds)) {
  34.         foreach ($i in 0..($pnReturnIds - 1)) {
  35.             $SPPC::SLUninstallLicense($handle, [System.Int64]$ppReturnIds + [System.Int64]16 * $i) | Out-Null
  36.         }   
  37.     }
  38. }

  39. $OSPP = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\OfficeSoftwareProtectionPlatform" -ErrorAction SilentlyContinue).Path
  40. if ($OSPP) {
  41.     UninstallLicenses($OSPP + "osppc.dll")
  42. }
  43. UninstallLicenses("sppc.dll")
  44. :embed:
复制代码


发表于 2023-3-4 21:26:45 | 显示全部楼层

#Include <WinAPI.au3>
Global $DLL = DllOpen("sppc.dll")
UninstallLicenses()
Func UninstallLicenses()
        If RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\OfficeSoftwareProtectionPlatform", "Path") = '' Then Return
        Local $Handle = SLOpen()
        If $Handle Then
                Local $pnReturnIds = 0, $ppReturnIds = 0
                If Not SLGetSLIDList($Handle, 0, _WinAPI_GUIDFromString("0ff1ce15-a989-479d-af46-f275c6370663"), 6, $pnReturnIds, $ppReturnIds) Then
                        For $i = 0 To $pnReturnIds - 1
                                $SPPC = SLUninstallLicense($Handle, $ppReturnIds + 16 * $i)
                        Next
                EndIf
                SLClose($Handle)
        EndIf
EndFunc   ;==>UninstallLicenses
Func SLOpen()
        Local $Ret = DllCall($DLL, 'int', 'SLOpen', 'Handle*', 0)
        If @error Then Return SetError(@error, 0, -1)
        Return SetError(GetLastError(), 0, $Ret[1])
EndFunc   ;==>SLOpen
Func SLGetSLIDList($hSLC, $eQueryIdType, $pQueryId, $eReturnIdType, ByRef $pnReturnIds, ByRef $ppReturnIds)
        Local $Ret = DllCall($DLL, 'int', 'SLGetSLIDList', 'Handle', $hSLC, 'int', $eQueryIdType, 'struct*', $pQueryId, 'int', $eReturnIdType, 'UINT*', 0, 'ptr*', 0)
        If @error Then Return SetError(@error, 0, -1)
        $pnReturnIds = $Ret[5]
        $ppReturnIds = $Ret[6]
        Return SetError(GetLastError(), 0, $Ret[0])
EndFunc   ;==>SLGetSLIDList
Func SLUninstallLicense($hSLC, $pLicenseFileId)
        Local $Ret = DllCall($DLL, 'int', 'SLUninstallLicense', 'Handle', $hSLC, 'ptr', $pLicenseFileId)
        If @error Then Return SetError(@error, 0, -1)
        Return SetError(GetLastError(), 0, $Ret[0])
EndFunc   ;==>SLUninstallLicense
Func SLClose($hSLC)
        Local $Ret = DllCall($DLL, 'int', 'SLClose', 'Handle', $hSLC)
        If @error Then Return SetError(@error, 0, -1)
        Return SetError(GetLastError(), 0, $Ret[0])
EndFunc   ;==>SLClose
Func GetLastError()
        Local $Ret = DllCall("kernel32.dll", "dword", "GetLastError")
        Return $Ret[0]
EndFunc   ;==>GetLastError



回复 支持 1 反对 0

使用道具 举报

发表于 2023-3-3 08:31:46 | 显示全部楼层
看不懂,直接说啥功能
 楼主| 发表于 2023-3-4 15:36:21 | 显示全部楼层
haijie1223 发表于 2023-3-3 08:31
看不懂,直接说啥功能

卸载office的许可证
 楼主| 发表于 2023-3-4 16:22:12 | 显示全部楼层
这是C代码:


  1. using System;
  2. using System.Runtime.InteropServices;

  3. public class CleanOffice
  4. {
  5.     private unsafe delegate uint SLOpenDelegate(void** phSLC);

  6.     private unsafe delegate uint SLGetSLIDListDelegate(void* hSLC, int eQueryIdType, Guid* pQueryId, int eReturnIdType, uint* pnReturnIds, Guid** ppReturnIds);

  7.     private unsafe delegate uint SLUninstallLicenseDelegate(void* hSLC, Guid* pLicenseFileId);

  8.     public static void Main()
  9.     {
  10.         UninstallLicenses("sppc");
  11.         string ospp = (string)Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\OfficeSoftwareProtectionPlatform", "Path", null);
  12.         if (ospp != null)
  13.         {
  14.             Console.WriteLine("Found Office Software Protection installed, cleaning");
  15.             UninstallLicenses(ospp + "osppc.dll");
  16.         }
  17.     }

  18.     [DllImport("kernel32.dll")]
  19.     private static extern IntPtr LoadLibrary(string path);

  20.     [DllImport("kernel32.dll")]
  21.     private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

  22.     private static void UninstallLicenses(string dllPath)
  23.     {
  24.         IntPtr sppc = LoadLibrary(dllPath);
  25.         SLOpenDelegate open = (SLOpenDelegate)Marshal.GetDelegateForFunctionPointer(GetProcAddress(sppc, "SLOpen"), typeof(SLOpenDelegate));
  26.         SLGetSLIDListDelegate getSLIDList = (SLGetSLIDListDelegate)Marshal.GetDelegateForFunctionPointer(GetProcAddress(sppc, "SLGetSLIDList"), typeof(SLGetSLIDListDelegate));
  27.         SLUninstallLicenseDelegate uninstallLicense = (SLUninstallLicenseDelegate)Marshal.GetDelegateForFunctionPointer(GetProcAddress(sppc, "SLUninstallLicense"), typeof(SLUninstallLicenseDelegate));

  28.         unsafe
  29.         {
  30.             void* phSLC;
  31.             open(&phSLC);
  32.             uint pnReturnIds;
  33.             Guid* ppReturnIds;
  34.             Guid officeGuid = new Guid("0ff1ce15-a989-479d-af46-f275c6370663");
  35.             if (getSLIDList(phSLC, 0, &officeGuid, 6, &pnReturnIds, &ppReturnIds) != 0)
  36.             {
  37.                 return;
  38.             }

  39.             for (int i = 0; i < pnReturnIds; i++)
  40.             {
  41.                 Console.WriteLine("Uninstalling license file " + ppReturnIds[i]);
  42.                 uninstallLicense(phSLC, &ppReturnIds[i]);
  43.             }
  44.         }
  45.     }
  46. }
复制代码


 楼主| 发表于 2023-3-4 22:48:15 | 显示全部楼层

貌似不太好使~但是没有报错。使用源代码,可以清除office的证书
发表于 2023-3-5 23:27:27 | 显示全部楼层
zbezj 发表于 2023-3-4 22:48
貌似不太好使~但是没有报错。使用源代码,可以清除office的证书

我自己的office证书都被清理干净了,你竟然说没用。。。
 楼主| 发表于 2023-3-6 20:08:18 | 显示全部楼层
haijie1223 发表于 2023-3-5 23:27
我自己的office证书都被清理干净了,你竟然说没用。。。

你是什么系统,office版本?我的是win11 x64, Office 2021
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|小黑屋|手机版|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2023-3-22 16:40 , Processed in 1.206693 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表