1

Тема: AHK: Передать объект из PowerShell

Есть программа которая предоставляет своё API только скриптам PowerShell которые загружают её dll, или C# приложениям которые устанавливаются в неё в виде плагинов. Есть ли какая то возможность передать в анк экземпляр API?

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

2

Re: AHK: Передать объект из PowerShell

Так как PowerShell  это .NET, то, наверное, можно через ahk .NET Framework.

3

Re: AHK: Передать объект из PowerShell

Да, только в каком именно направлении двигать.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

4

Re: AHK: Передать объект из PowerShell

https://www.autohotkey.com/boards/viewt … 52#p288852

5

Re: AHK: Передать объект из PowerShell

CLR я видел, но там C#, а он выполняется видимо как то "внутри" программы.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

6

Re: AHK: Передать объект из PowerShell

Не понимаю, что ты имеешь в виду.
На C# пишется повершелл враппер, который потом вызывается из ахк.
У меня данный код работает:

cSharp =
(
using System.Text;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

public class ps
{
	public string RunScript(string scriptText)
	{
		Runspace runspace = RunspaceFactory.CreateRunspace();
		runspace.Open();

		Pipeline pipeline = runspace.CreatePipeline();
		pipeline.Commands.AddScript(scriptText);
		pipeline.Commands.Add("Out-String");

		Collection<PSObject> results = pipeline.Invoke();

		runspace.Close();
		return results[0].ToString();
		
		/*
		StringBuilder stringBuilder = new StringBuilder();
		foreach (PSObject obj in results)
		{
			stringBuilder.AppendLine(obj.ToString());
		}

		return stringBuilder.ToString();
		*/
	}
}
)

asm := CLR_CompileC#( cSharp, "System.Core.dll | C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll" )
ps := asm.CreateInstance("ps")
MsgBox % ps.RunScript("echo hello")
MsgBox % ps.RunScript("Get-Service")

/*
psCode =
(
[Reflection.Assembly]::LoadFile("D:\Desktop\EPPlus.dll") | Out-Null
$xl = New-Object OfficeOpenXml.ExcelPackage
$ws = $xl.Workbook.Worksheets.Add("test")
$ws.Cells["A1"].Value = 123
$ws.Cells["A2"].Value = "test"
$xl.SaveAs("CreateByPS.xlsx")
)
msgbox % ps.RunScript(psCode)
*/


; ==========================================================
;                  .NET Framework Interop
;      https://autohotkey.com/boards/viewtopic.php?t=4633
; ==========================================================
;
;   Author:     Lexikos
;   Version:    1.2
;   Requires:	AutoHotkey_L v1.0.96+
;

CLR_LoadLibrary(AssemblyName, AppDomain=0)
{
	if !AppDomain
		AppDomain := CLR_GetDefaultDomain()
	e := ComObjError(0)
	Loop 1 {
		if assembly := AppDomain.Load_2(AssemblyName)
			break
		static null := ComObject(13,0)
		args := ComObjArray(0xC, 1),  args[0] := AssemblyName
		typeofAssembly := AppDomain.GetType().Assembly.GetType()
		if assembly := typeofAssembly.InvokeMember_3("LoadWithPartialName", 0x158, null, null, args)
			break
		if assembly := typeofAssembly.InvokeMember_3("LoadFrom", 0x158, null, null, args)
			break
	}
	ComObjError(e)
	return assembly
}

CLR_CreateObject(Assembly, TypeName, Args*)
{
	if !(argCount := Args.MaxIndex())
		return Assembly.CreateInstance_2(TypeName, true)
	
	vargs := ComObjArray(0xC, argCount)
	Loop % argCount
		vargs[A_Index-1] := Args[A_Index]
	
	static Array_Empty := ComObjArray(0xC,0), null := ComObject(13,0)
	
	return Assembly.CreateInstance_3(TypeName, true, 0, null, vargs, null, Array_Empty)
}

CLR_CompileC#(Code, References="", AppDomain=0, FileName="", CompilerOptions="")
{
	return CLR_CompileAssembly(Code, References, "System", "Microsoft.CSharp.CSharpCodeProvider", AppDomain, FileName, CompilerOptions)
}

CLR_CompileVB(Code, References="", AppDomain=0, FileName="", CompilerOptions="")
{
	return CLR_CompileAssembly(Code, References, "System", "Microsoft.VisualBasic.VBCodeProvider", AppDomain, FileName, CompilerOptions)
}

CLR_StartDomain(ByRef AppDomain, BaseDirectory="")
{
	static null := ComObject(13,0)
	args := ComObjArray(0xC, 5), args[0] := "", args[2] := BaseDirectory, args[4] := ComObject(0xB,false)
	AppDomain := CLR_GetDefaultDomain().GetType().InvokeMember_3("CreateDomain", 0x158, null, null, args)
	return A_LastError >= 0
}

CLR_StopDomain(ByRef AppDomain)
{	; ICorRuntimeHost::UnloadDomain
	DllCall("SetLastError", "uint", hr := DllCall(NumGet(NumGet(0+RtHst:=CLR_Start())+20*A_PtrSize), "ptr", RtHst, "ptr", ComObjValue(AppDomain))), AppDomain := ""
	return hr >= 0
}

; NOTE: IT IS NOT NECESSARY TO CALL THIS FUNCTION unless you need to load a specific version.
CLR_Start(Version="") ; returns ICorRuntimeHost*
{
	static RtHst := 0
	; The simple method gives no control over versioning, and seems to load .NET v2 even when v4 is present:
	; return RtHst ? RtHst : (RtHst:=COM_CreateObject("CLRMetaData.CorRuntimeHost","{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}"), DllCall(NumGet(NumGet(RtHst+0)+40),"uint",RtHst))
	if RtHst
		return RtHst
	EnvGet SystemRoot, SystemRoot
	if Version =
		Loop % SystemRoot "\Microsoft.NET\Framework" (A_PtrSize=8?"64":"") "\*", 2
			if (FileExist(A_LoopFileFullPath "\mscorlib.dll") && A_LoopFileName > Version)
				Version := A_LoopFileName
	if DllCall("mscoree\CorBindToRuntimeEx", "wstr", Version, "ptr", 0, "uint", 0
	, "ptr", CLR_GUID(CLSID_CorRuntimeHost, "{CB2F6723-AB3A-11D2-9C40-00C04FA30A3E}")
	, "ptr", CLR_GUID(IID_ICorRuntimeHost,  "{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}")
	, "ptr*", RtHst) >= 0
		DllCall(NumGet(NumGet(RtHst+0)+10*A_PtrSize), "ptr", RtHst) ; Start
	return RtHst
}

;
; INTERNAL FUNCTIONS
;

CLR_GetDefaultDomain()
{
	static defaultDomain := 0
	if !defaultDomain
	{	; ICorRuntimeHost::GetDefaultDomain
		if DllCall(NumGet(NumGet(0+RtHst:=CLR_Start())+13*A_PtrSize), "ptr", RtHst, "ptr*", p:=0) >= 0
			defaultDomain := ComObject(p), ObjRelease(p)
	}
	return defaultDomain
}

CLR_CompileAssembly(Code, References, ProviderAssembly, ProviderType, AppDomain=0, FileName="", CompilerOptions="")
{
	if !AppDomain
		AppDomain := CLR_GetDefaultDomain()
	
	if !(asmProvider := CLR_LoadLibrary(ProviderAssembly, AppDomain))
	|| !(codeProvider := asmProvider.CreateInstance(ProviderType))
	|| !(codeCompiler := codeProvider.CreateCompiler())
		return 0

	if !(asmSystem := (ProviderAssembly="System") ? asmProvider : CLR_LoadLibrary("System", AppDomain))
		return 0
	
	; Convert | delimited list of references into an array.
	StringSplit, Refs, References, |, %A_Space%%A_Tab%
	aRefs := ComObjArray(8, Refs0)
	Loop % Refs0
		aRefs[A_Index-1] := Refs%A_Index%
	
	; Set parameters for compiler.
	prms := CLR_CreateObject(asmSystem, "System.CodeDom.Compiler.CompilerParameters", aRefs)
	, prms.OutputAssembly          := FileName
	, prms.GenerateInMemory        := FileName=""
	, prms.GenerateExecutable      := SubStr(FileName,-3)=".exe"
	, prms.CompilerOptions         := CompilerOptions
	, prms.IncludeDebugInformation := true
	
	; Compile!
	compilerRes := codeCompiler.CompileAssemblyFromSource(prms, Code)
	
	if error_count := (errors := compilerRes.Errors).Count
	{
		error_text := ""
		Loop % error_count
			error_text .= ((e := errors.Item[A_Index-1]).IsWarning ? "Warning " : "Error ") . e.ErrorNumber " on line " e.Line ": " e.ErrorText "`n`n"
		MsgBox, 16, Compilation Failed, %error_text%
		return 0
	}
	; Success. Return Assembly object or path.
	return compilerRes[FileName="" ? "CompiledAssembly" : "PathToAssembly"]
}

CLR_GUID(ByRef GUID, sGUID)
{
	VarSetCapacity(GUID, 16, 0)
	return DllCall("ole32\CLSIDFromString", "wstr", sGUID, "ptr", &GUID) >= 0 ? &GUID : ""
}

Еще можно пробовать через ActiveXPosh:
http://forum.script-coding.com/viewtopic.php?id=2816

7

Re: AHK: Передать объект из PowerShell

Пропустил сообщение.
Не понимаю как это должно работать. Твой пример работает, но например одна простая строка не работает

[System.Windows.Forms.MessageBox]::Show('Message','Title',0)

Но в Windows PowerShell ISE (x86) работает.


cSharp =
( 
using System.Text;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

public class ps
{
	public string RunScript(string scriptText)
	{
		Runspace runspace = RunspaceFactory.CreateRunspace();
		runspace.Open();

		Pipeline pipeline = runspace.CreatePipeline();
		pipeline.Commands.AddScript(scriptText);
		pipeline.Commands.Add("Out-String");

		Collection<PSObject> results = pipeline.Invoke();

		runspace.Close();
		return results[0].ToString();
		
		/*
		StringBuilder stringBuilder = new StringBuilder();
		foreach (PSObject obj in results)
		{
			stringBuilder.AppendLine(obj.ToString());
		}

		return stringBuilder.ToString();
		*/
	}
}
)

asm := CLR_CompileC#( cSharp, "System.Core.dll | C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll" )
ps := asm.CreateInstance("ps")

psCode =
(
[System.Windows.Forms.MessageBox]::Show('Message','Title',0)
) 
ps.RunScript(psCode) 
return
По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

8

Re: AHK: Передать объект из PowerShell

Попробуй спросить на оф.форуме у tmplinshi. Я, просто, не знаю  C#.

9

Re: AHK: Передать объект из PowerShell

Добрался до компьютера.
У меня так работает:

psCode =
(
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show('Message','Title',0)
)
ps.RunScript(psCode)

10

Re: AHK: Передать объект из PowerShell

Спасибо, а не подскажешь в чём отличие от консоли PS.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

11

Re: AHK: Передать объект из PowerShell

У меня отличий нету, так как в консоли PS, без этой строки выдает ошибку.

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

Но я powershell никогда не использовал и не разбирался как у него всё работает.

12 (изменено: EV, 2019-12-16 22:19:10)

Re: AHK: Передать объект из PowerShell

Для примера.

RunWait %ComSpec% /c F:\Kodi\texturecache.py-master\texturecache.py  P,, Hide

13

Re: AHK: Передать объект из PowerShell

Это не powershell, спутал с питоном. RunWait команда

14

Re: AHK: Передать объект из PowerShell

Кстати, консоль PS и PowerShell ISE две разные вещи.

15

Re: AHK: Передать объект из PowerShell

 Run, powershell -Command "Get-ChildItem -Path '%FullPath%'  -Recurse | Unblock-File"