<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: Передать объект из PowerShell]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=15030</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=15030&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: Передать объект из PowerShell».]]></description>
		<lastBuildDate>Fri, 27 Dec 2019 20:25:15 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=137078#p137078</link>
			<description><![CDATA[<div class="codebox"><pre><code> Run, powershell -Command &quot;Get-ChildItem -Path &#039;%FullPath%&#039;  -Recurse | Unblock-File&quot; </code></pre></div>]]></description>
			<author><![CDATA[null@example.com (EV)]]></author>
			<pubDate>Fri, 27 Dec 2019 20:25:15 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=137078#p137078</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136911#p136911</link>
			<description><![CDATA[<p>Кстати, консоль PS и PowerShell ISE две разные вещи.</p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Mon, 16 Dec 2019 20:15:06 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136911#p136911</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136908#p136908</link>
			<description><![CDATA[<p>Это не powershell, спутал с питоном. RunWait команда</p>]]></description>
			<author><![CDATA[null@example.com (EV)]]></author>
			<pubDate>Mon, 16 Dec 2019 18:37:36 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136908#p136908</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136906#p136906</link>
			<description><![CDATA[<p>Для примера. <br /></p><div class="codebox"><pre><code>RunWait %ComSpec% /c F:\Kodi\texturecache.py-master\texturecache.py  P,, Hide</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (EV)]]></author>
			<pubDate>Mon, 16 Dec 2019 18:16:59 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136906#p136906</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136492#p136492</link>
			<description><![CDATA[<p>У меня отличий нету, так как в консоли PS, без этой строки выдает ошибку.<br /></p><div class="codebox"><pre><code>[System.Reflection.Assembly]::LoadWithPartialName(&quot;System.Windows.Forms&quot;)</code></pre></div><p>Но я powershell никогда не использовал и не разбирался как у него всё работает.</p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Tue, 29 Oct 2019 19:36:17 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136492#p136492</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136489#p136489</link>
			<description><![CDATA[<p>Спасибо, а не подскажешь в чём отличие от консоли PS.</p>]]></description>
			<author><![CDATA[null@example.com (serzh82saratov)]]></author>
			<pubDate>Tue, 29 Oct 2019 18:44:47 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136489#p136489</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136487#p136487</link>
			<description><![CDATA[<p>Добрался до компьютера.<br />У меня так работает:<br /></p><div class="codebox"><pre><code>psCode =
(
[System.Reflection.Assembly]::LoadWithPartialName(&quot;System.Windows.Forms&quot;)
[System.Windows.Forms.MessageBox]::Show(&#039;Message&#039;,&#039;Title&#039;,0)
)
ps.RunScript(psCode)</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Tue, 29 Oct 2019 12:26:45 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136487#p136487</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136482#p136482</link>
			<description><![CDATA[<p>Попробуй спросить на оф.форуме у tmplinshi. Я, просто, не знаю&nbsp; C#.</p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Tue, 29 Oct 2019 01:59:59 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136482#p136482</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136469#p136469</link>
			<description><![CDATA[<p>Пропустил сообщение. <br />Не понимаю как это должно работать. Твой пример работает, но например одна простая строка не работает</p><div class="codebox"><pre><code>[System.Windows.Forms.MessageBox]::Show(&#039;Message&#039;,&#039;Title&#039;,0)</code></pre></div><p>Но в Windows PowerShell ISE (x86) работает.</p><div class="codebox"><pre><code>
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(&quot;Out-String&quot;);

		Collection&lt;PSObject&gt; 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, &quot;System.Core.dll | C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll&quot; )
ps := asm.CreateInstance(&quot;ps&quot;)

psCode =
(
[System.Windows.Forms.MessageBox]::Show(&#039;Message&#039;,&#039;Title&#039;,0)
) 
ps.RunScript(psCode) 
return
</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (serzh82saratov)]]></author>
			<pubDate>Sun, 27 Oct 2019 17:14:03 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136469#p136469</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136435#p136435</link>
			<description><![CDATA[<p>Не понимаю, что ты имеешь в виду.<br />На C# пишется повершелл враппер, который потом вызывается из ахк.<br />У меня данный код работает:<br /></p><div class="codebox"><pre><code>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(&quot;Out-String&quot;);

		Collection&lt;PSObject&gt; 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, &quot;System.Core.dll | C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll&quot; )
ps := asm.CreateInstance(&quot;ps&quot;)
MsgBox % ps.RunScript(&quot;echo hello&quot;)
MsgBox % ps.RunScript(&quot;Get-Service&quot;)

/*
psCode =
(
[Reflection.Assembly]::LoadFile(&quot;D:\Desktop\EPPlus.dll&quot;) | Out-Null
$xl = New-Object OfficeOpenXml.ExcelPackage
$ws = $xl.Workbook.Worksheets.Add(&quot;test&quot;)
$ws.Cells[&quot;A1&quot;].Value = 123
$ws.Cells[&quot;A2&quot;].Value = &quot;test&quot;
$xl.SaveAs(&quot;CreateByPS.xlsx&quot;)
)
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(&quot;LoadWithPartialName&quot;, 0x158, null, null, args)
			break
		if assembly := typeofAssembly.InvokeMember_3(&quot;LoadFrom&quot;, 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=&quot;&quot;, AppDomain=0, FileName=&quot;&quot;, CompilerOptions=&quot;&quot;)
{
	return CLR_CompileAssembly(Code, References, &quot;System&quot;, &quot;Microsoft.CSharp.CSharpCodeProvider&quot;, AppDomain, FileName, CompilerOptions)
}

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

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

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

; NOTE: IT IS NOT NECESSARY TO CALL THIS FUNCTION unless you need to load a specific version.
CLR_Start(Version=&quot;&quot;) ; 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(&quot;CLRMetaData.CorRuntimeHost&quot;,&quot;{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}&quot;), DllCall(NumGet(NumGet(RtHst+0)+40),&quot;uint&quot;,RtHst))
	if RtHst
		return RtHst
	EnvGet SystemRoot, SystemRoot
	if Version =
		Loop % SystemRoot &quot;\Microsoft.NET\Framework&quot; (A_PtrSize=8?&quot;64&quot;:&quot;&quot;) &quot;\*&quot;, 2
			if (FileExist(A_LoopFileFullPath &quot;\mscorlib.dll&quot;) &amp;&amp; A_LoopFileName &gt; Version)
				Version := A_LoopFileName
	if DllCall(&quot;mscoree\CorBindToRuntimeEx&quot;, &quot;wstr&quot;, Version, &quot;ptr&quot;, 0, &quot;uint&quot;, 0
	, &quot;ptr&quot;, CLR_GUID(CLSID_CorRuntimeHost, &quot;{CB2F6723-AB3A-11D2-9C40-00C04FA30A3E}&quot;)
	, &quot;ptr&quot;, CLR_GUID(IID_ICorRuntimeHost,  &quot;{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}&quot;)
	, &quot;ptr*&quot;, RtHst) &gt;= 0
		DllCall(NumGet(NumGet(RtHst+0)+10*A_PtrSize), &quot;ptr&quot;, 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), &quot;ptr&quot;, RtHst, &quot;ptr*&quot;, p:=0) &gt;= 0
			defaultDomain := ComObject(p), ObjRelease(p)
	}
	return defaultDomain
}

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

	if !(asmSystem := (ProviderAssembly=&quot;System&quot;) ? asmProvider : CLR_LoadLibrary(&quot;System&quot;, 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, &quot;System.CodeDom.Compiler.CompilerParameters&quot;, aRefs)
	, prms.OutputAssembly          := FileName
	, prms.GenerateInMemory        := FileName=&quot;&quot;
	, prms.GenerateExecutable      := SubStr(FileName,-3)=&quot;.exe&quot;
	, prms.CompilerOptions         := CompilerOptions
	, prms.IncludeDebugInformation := true
	
	; Compile!
	compilerRes := codeCompiler.CompileAssemblyFromSource(prms, Code)
	
	if error_count := (errors := compilerRes.Errors).Count
	{
		error_text := &quot;&quot;
		Loop % error_count
			error_text .= ((e := errors.Item[A_Index-1]).IsWarning ? &quot;Warning &quot; : &quot;Error &quot;) . e.ErrorNumber &quot; on line &quot; e.Line &quot;: &quot; e.ErrorText &quot;`n`n&quot;
		MsgBox, 16, Compilation Failed, %error_text%
		return 0
	}
	; Success. Return Assembly object or path.
	return compilerRes[FileName=&quot;&quot; ? &quot;CompiledAssembly&quot; : &quot;PathToAssembly&quot;]
}

CLR_GUID(ByRef GUID, sGUID)
{
	VarSetCapacity(GUID, 16, 0)
	return DllCall(&quot;ole32\CLSIDFromString&quot;, &quot;wstr&quot;, sGUID, &quot;ptr&quot;, &amp;GUID) &gt;= 0 ? &amp;GUID : &quot;&quot;
}</code></pre></div><p>Еще можно пробовать через ActiveXPosh:<br /><a href="http://forum.script-coding.com/viewtopic.php?id=2816">http://forum.script-coding.com/viewtopic.php?id=2816</a></p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Mon, 21 Oct 2019 10:20:02 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136435#p136435</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136434#p136434</link>
			<description><![CDATA[<p>CLR я видел, но там C#, а он выполняется видимо как то &quot;внутри&quot; программы.</p>]]></description>
			<author><![CDATA[null@example.com (serzh82saratov)]]></author>
			<pubDate>Mon, 21 Oct 2019 10:08:24 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136434#p136434</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136432#p136432</link>
			<description><![CDATA[<p><a href="https://www.autohotkey.com/boards/viewtopic.php?p=288852#p288852">https://www.autohotkey.com/boards/viewt … 52#p288852</a></p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Mon, 21 Oct 2019 06:45:39 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136432#p136432</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136431#p136431</link>
			<description><![CDATA[<p>Да, только в каком именно направлении двигать.</p>]]></description>
			<author><![CDATA[null@example.com (serzh82saratov)]]></author>
			<pubDate>Mon, 21 Oct 2019 06:21:28 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136431#p136431</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136429#p136429</link>
			<description><![CDATA[<p>Так как PowerShell&nbsp; это .NET, то, наверное, можно через ahk .NET Framework.</p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Mon, 21 Oct 2019 00:12:16 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136429#p136429</guid>
		</item>
		<item>
			<title><![CDATA[AHK: Передать объект из PowerShell]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=136428#p136428</link>
			<description><![CDATA[<p>Есть программа которая предоставляет своё API только скриптам PowerShell которые загружают её dll, или C# приложениям которые устанавливаются в неё в виде плагинов. Есть ли какая то возможность передать в анк экземпляр API?</p>]]></description>
			<author><![CDATA[null@example.com (serzh82saratov)]]></author>
			<pubDate>Sun, 20 Oct 2019 22:47:49 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=136428#p136428</guid>
		</item>
	</channel>
</rss>
