- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Powershell 中添加类型。我正在使用 netapi32.dll 使用 NetSessionEnum 函数。
这几乎有效,但不完全有效。当以零级查询时,我可以获得结果。当使用任何其他选项查询时,IDE 或 powershell session 在尝试对 PtrToStructure 进行编码时只会崩溃。我完全卡住了?
我认为这与结构尺寸有关,但不确定。
如果有人能指出我正确的方向或提供一些技巧,那就太棒了。
NetSessionEnum function
function Get-NetSessions {
param(
[string]$ComputerName = "",
[string]$ComputerSession = "",
[string]$UserName = "",
[int]$QueryLevel
)
$DebugPreference = 'continue'
$signature = @'
[DllImport("netapi32.dll", SetLastError=true)]
public static extern int NetSessionEnum(
[In,MarshalAs(UnmanagedType.LPWStr)] string ServerName,
[In,MarshalAs(UnmanagedType.LPWStr)] string UncClientName,
[In,MarshalAs(UnmanagedType.LPWStr)] string UserName,
Int32 Level,
out IntPtr bufptr,
int prefmaxlen,
ref Int32 entriesread,
ref Int32 totalentries,
ref Int32 resume_handle);
'@
$SessionInfoStructures = @'
namespace pinvoke {
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SESSION_INFO_0
{
[MarshalAs(UnmanagedType.LPWStr)]
public String sesi0_cname;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SESSION_INFO_1
{
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi1_cname;
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi1_username;
public uint sesi1_num_opens;
public uint sesi1_time;
public uint sesi1_idle_time;
public uint sesi1_user_flag;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SESSION_INFO_2
{
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi2_cname;
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi2_username;
public uint sesi2_num_opens;
public uint sesi2_time;
public uint sesi2_idle_time;
public uint sesi2_user_flags;
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi2_cltype_name;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SESSION_INFO_10
{
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi10_cname;
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi10_username;
public uint sesi10_time;
public uint sesi10_idle_time;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SESSION_INFO_502
{
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi502_cname;
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi502_username;
public uint sesi502_num_opens;
public uint sesi502_time;
public uint sesi502_idle_time;
public uint sesi502_user_flags;
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi502_cltype_name;
[MarshalAs(UnmanagedType.LPWStr)]
public string sesi502_transport;
}
public enum NERR
{
/// <summary>
/// Operation was a success.
/// </summary>
NERR_Success = 0,
/// <summary>
/// More data available to read. dderror getting all data.
/// </summary>
ERROR_MORE_DATA = 234,
/// <summary>
/// Network browsers not available.
/// </summary>
ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
/// <summary>
/// LEVEL specified is not valid for this call.
/// </summary>
ERROR_INVALID_LEVEL = 124,
/// <summary>
/// Security context does not have permission to make this call.
/// </summary>
ERROR_ACCESS_DENIED = 5,
/// <summary>
/// Parameter was incorrect.
/// </summary>
ERROR_INVALID_PARAMETER = 87,
/// <summary>
/// Out of memory.
/// </summary>
ERROR_NOT_ENOUGH_MEMORY = 8,
/// <summary>
/// Unable to contact resource. Connection timed out.
/// </summary>
ERROR_NETWORK_BUSY = 54,
/// <summary>
/// Network Path not found.
/// </summary>
ERROR_BAD_NETPATH = 53,
/// <summary>
/// No available network connection to make call.
/// </summary>
ERROR_NO_NETWORK = 1222,
/// <summary>
/// Pointer is not valid.
/// </summary>
ERROR_INVALID_HANDLE_STATE = 1609,
/// <summary>
/// Extended Error.
/// </summary>
ERROR_EXTENDED_ERROR= 1208,
/// <summary>
/// Base.
/// </summary>
NERR_BASE = 2100,
/// <summary>
/// Unknown Directory.
/// </summary>
NERR_UnknownDevDir = (NERR_BASE + 16),
/// <summary>
/// Duplicate Share already exists on server.
/// </summary>
NERR_DuplicateShare = (NERR_BASE + 18),
/// <summary>
/// Memory allocation was to small.
/// </summary>
NERR_BufTooSmall = (NERR_BASE + 23)
}
public enum SESSION_LEVEL
{
/// <summary>
/// ZERO
/// </summary>
LEVEL_0 = 0,
/// <summary>
/// ONE
/// </summary>
LEVEL_1 = 1,
/// <summary>
/// TWO
/// </summary>
LEVEL_2 = 2,
/// <summary>
/// TEN
/// </summary>
LEVEL_10 = 10,
/// <summary>
/// FIVE HUNDRED AND TWO
/// </summary>
LEVEL_502 = 502
}
}
'@
# Add the custom structures and enums
Add-Type $SessionInfoStructures
# Add the function definition
Add-Type -MemberDefinition $signature -Name Win32Util -Namespace Pinvoke -Using Pinvoke
if ([Pinvoke.SESSION_LEVEL]::LEVEL_0 -eq $QueryLevel) {$x = New-Object pinvoke.SESSION_INFO_0}
if ([Pinvoke.SESSION_LEVEL]::LEVEL_1 -eq $QueryLevel) {$x = New-Object pinvoke.SESSION_INFO_1}
if ([Pinvoke.SESSION_LEVEL]::LEVEL_2 -eq $QueryLevel) {$x = New-Object pinvoke.SESSION_INFO_2}
if ([Pinvoke.SESSION_LEVEL]::LEVEL_10 -eq $QueryLevel) {$x = New-Object pinvoke.SESSION_INFO_10}
if ([Pinvoke.SESSION_LEVEL]::LEVEL_502 -eq $QueryLevel) {$x = New-Object pinvoke.SESSION_INFO_502}
# Declare the reference variables
$type = $x.gettype()
Write-Debug "$type.tostring()"
$ptrInfo = 0
$EntriesRed = 0
$TotalRead = 0
$ResumeHandle = 0
# Call the function
$Result = [pinvoke.Win32Util]::NetSessionEnum($ComputerName,$ComputerSession,$UserName,0,[ref]$ptrInfo,-1,[ref]$EntriesRed,[ref]$TotalRead,[ref]$ResumeHandle)
$Result
if ($Result -eq ([pinvoke.NERR]::NERR_Success)){
Write-Debug 'Result is success'
Write-Debug "IntPtr $ptrInfo"
Write-Debug "Entries read $EntriesRed"
Write-Debug "Total Read $TotalRead"
# Locate the offset of the initial intPtr
$offset = $ptrInfo.ToInt64()
Write-Debug "Starting Offset $offset"
# Work out how mutch to increment the pointer by finding out the size of the structure
$Increment = [System.Runtime.Interopservices.Marshal]::SizeOf($x)
Write-Debug "Increment $Increment"
for ($i = 0; ($i -lt $EntriesRed); $i++){
$newintptr = New-Object system.Intptr -ArgumentList $offset
Write-Debug "Newintptr `[$i`] $newintptr"
$Info = [system.runtime.interopservices.marshal]::PtrToStructure($newintptr,$type)
$Info | Select-Object *
$offset = $newintptr.ToInt64()
$offset += $increment
}
}
else
{
# switch ($Result)
# {
# ([Pinvoke.NERR]::ERROR_ACCESS_DENIED) {Write-Host "The user does not have access to the requested information."}
# ([Pinvoke.NERR]::ERROR_INVALID_LEVEL) {Write-Host "The value specified for the level parameter is not valid."}
# ([Pinvoke.NERR]::ERROR_INVALID_PARAMETER) {Write-Host 'The specified parameter is not valid.'}
# ([Pinvoke.NERR]::ERROR_MORE_DATA) {Write-Host 'More entries are available. Specify a large enough buffer to receive all entries.'}
# ([Pinvoke.NERR]::ERROR_NOT_ENOUGH_MEMORY) {Write-Host 'Insufficient memory is available.'}
# ([Pinvoke.NERR]::NERR_ClientNameNotFound) {Write-Host 'A session does not exist with the computer name.'}
# ([Pinvoke.NERR]::NERR_InvalidComputer) {Write-Host 'The computer name is not valid.'}
# ([Pinvoke.NERR]::NERR_ClientNameNotFound) {Write-Host 'The user name could not be found.'}
# }
}
}
Get-NetSessions -QueryLevel 0
最佳答案
我早就放弃了上述内容并转向其他事情,直到某件事再次引起了我的兴趣。
https://support.microsoft.com/en-us/help/2909958/exceptions-in-windows-powershell-other-dynamic-languages-and-dynamical
添加这个非常小的修改时,上面的代码工作(没有抛出错误):
$Info = [system.runtime.interopservices.marshal]::PtrToStructure($newintptr,$type)
到
$Info = [system.runtime.interopservices.marshal]::PtrToStructure($newintptr,[System.Type]$type)
如果您有兴趣使用 Windows API
this person 有一些有趣的工作,并链接到其他一些很棒的资源。
关于enums - 在 powershell 中使用 add-type 处理 intPtr 和编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12451246/
我不明白为什么这甚至可以编译。我尝试过不同的格式,它们似乎都有效。 为什么有一个 enum of enum of enum of.. 是合法的? interface I { enum E im
我有大型数据库(数百万行),我正在尝试为 2 个字段的数据类型做出最佳选择。我制作的大部分内容都是 varchar 或 INT。但是,我想知道 Enum 是否是最好的方法的 2 个字段。 字段 1第一
我是 C++ 新手,我想知道是否有人可以帮助我理解原因 enum difficulty { NOVICE, EASY, NORMAL, HARD, UNBEATABLE }; difficulty m
从 native 代码强制转换(在 C++/CLI 中)的正确方法是什么 enum到托管代码enum其中包含相同的 enum值(value)观?与使用 C# 的强制转换方式有什么区别,例如 (int)
我有以下枚举 [Flags] public enum WeekDays { Monday = 1, Tuesday = 2, Wednesday = 4, Thursd
我刚刚学习 Java 中的枚举。当我运行下面的代码时,我收到一个错误,我也在下面重现该错误。基本上,我的问题是:当我在枚举中定义一个方法,并且在该方法中我想检查枚举的值以便我可以根据该值执行某些操作时
我想要一些语法糖来打开 Enum .当然,一个if else块按预期工作: @enum Fruit apple=1 orange=2 kiwi=3 function talk1(fruit::Frui
我试图在 Enum.each 的函数内为变量设置一个值,但在循环结束时,变量为空,我不知道为什么会出现这种行为。 代码: base = "master" candidates = ["stream",
This question already has answers here: Is there a way to use existing structs as enum variants? (2个
我想让 Java 中的枚举器具有其他枚举作为属性。 public enum Direction { Up(Down), Down(Up), Left(Right), R
我有一个包含两种不同可能“类型”的枚举和一个可以返回其中任何一种的函数,封装在 Option 中: enum Possibilities { First(i32), Second(St
我刚刚注意到 pyhton 中 Enum+defaultdict 的一个非常奇怪的行为。我定义了一个这样的枚举,它收集了一些默认词典: from enum import Enum from colle
我想在运行时从配置文件生成一些类型。为简单起见,假设我已经将数据加载为 Python 字典: color_values = dict(RED = 1, YELLOW = 2, GREEN = 3) 我
我想创建一个方法,在传入参数的任何枚举类上实现 valueOf(并包装一些专门的错误/缺失名称代码)。所以基本上,我有几个枚举,例如: enum Enum1{ A, B, C } enum Enum2
我有一个user数据库表: CREATE TABLE IF NOT EXISTS `user` ( `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
这是我的 JPA @Repository,在这里我们可以得到 list与 findByFullNameContaining(String query) - 通过在查询中提供 fullName 的子字符
(也在这里打开了一个问题:https://github.com/square/moshi/issues/768 但被要求打开一个stackoverflow问题)我正在编写一个通用适配器来转换带有枚举值
请帮助找到以下情况的最佳方法: 有一个表,大约有 20 列。 每列都有自己的短名称、全名称和类型(数字或字符串)。 每个列类型都可以有自己的运算符 - 例如,字符串 - 包含、等于;数字 - 更多、更
如果我在 python 中按功能创建了 enum.Enum(本例中为 3.7),如何从中检索类的名称? import enum def print_the_enum_class_name(some_e
我正在编写一个与第 3 方程序交互的程序。这个第 3 方程序允许用户制作可以运行在第 3 方程序中进行的步骤记录的按钮。 但! 这些按钮还可以运行用户定义的批处理文件。因此,我使用此功能通过创建文件并
我是一名优秀的程序员,十分优秀!