Powershell脚本将输出系统硬件和软件规格输出到Excel

时间:2016-07-14 19:35:04

标签: powershell

我试图将已安装软件和软件版本的powershell程序(Get-IS)的结果合并到另一个PowerShell程序中,该程序从wmi调用(CompInv)创建excel电子表格。这两个程序都是由其他程序创建的,没有任何问题,但是我似乎无法从第一个程序中获取软件列表和版本信息以便正确输出,因此可以将其输入到Excel电子表格中。下面两个程序的代码(注意:Get-IS列表比Get-WmiObject或gp HKLM更多,这就是我使用它的原因)。我需要做些什么才能让它发挥作用?以下两个程序的代码

Get-IS执行此操作

#include <iostream>
#include <memory>
#include <string.h>

#ifdef _WIN64
#include <ws2tcpip.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif

#include <errno.h>

#define MAXWAIT 5000

bool readN(SOCKET fd, int size, char *buffer)
{
    fd_set readset;
    struct timeval tv;
    int left, res;

    FD_ZERO(&readset);
    FD_SET(fd, &readset);

    left = size;
    std::cout << "-----called readN to read " << size << " byte" << std::endl;
    while (left > 0) {
        tv.tv_sec = MAXWAIT;
        tv.tv_usec = 0;
        res = select(fd + 1, &readset, NULL, NULL, &tv);

        if (res > 0) {
            res = recv(fd, buffer, left, 0);
            if (res == 0) { //connection closed by client
                return false;
            }

            left -= res;
            std::cout << "\treceived " << res << " left " << left << std::endl;
            buffer += res;
        } else if (res == 0) {  //timer expired
            std::cout << "\ttimer expired" << std::endl;
            return false;
        } else {        //socket error
            std::cout << "\tsocket error " << WSAGetLastError()  << std::endl;
            return false;
        }
    }
    std::cout << "Print the buffer now\n" << (buffer - size) << std::endl;
    return true;
}

int main(void)
{
    int err;
    SOCKET cfd = 0;
    SOCKET afd = 0;

    struct sockaddr_in addr;
    socklen_t clen;
    struct sockaddr_in caddr;

#ifdef _WIN64
    WORD ver = 0x202;
    WSADATA wsa_data;

    memset(&wsa_data, 0, sizeof(wsa_data));
    std::cout << "WSAStartup" << std::endl;
    err = WSAStartup(ver, &wsa_data);
    if (err < 0) goto error_exit;
#endif

    memset(&addr, 0, sizeof(addr));
    memset(&caddr, 0, sizeof(caddr));

    std::cout << "socket" << std::endl;
    afd = socket(AF_INET, SOCK_STREAM, 0);
    if (afd < 0) goto error_exit;

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(1234);

    std::cout << "bind" << std::endl;
    err = bind(afd, (struct sockaddr *)&addr, sizeof(addr));
    if (err < 0) goto error_exit;

    std::cout << "listen" << std::endl;
    listen(afd, 5);

    clen = sizeof(caddr);
    std::cout << "accept" << std::endl;
    cfd = accept(afd, (struct sockaddr *) &caddr, &clen);
    if (cfd == INVALID_SOCKET) goto error_exit;

    {
        int size_ = 1024;
        std::unique_ptr<char[]> buffer2 = std::make_unique<char[]>(size_);

        std::cout << "readN" << std::endl;
        if (readN(cfd, 1024, buffer2.get())) {
            std::cout << "----read message----" << std::endl;
            std::cout <<"\t"<< buffer2.get() << std::endl;
        }
    }
    return 0;
error_exit:
    std::cout << "Error!" << std::endl;
    std::cout << "\tsocket error " << WSAGetLastError()  << std::endl;
    return 1;
}

CompInv这样做

    Param
    (
        [Alias('Computer','ComputerName','HostName')]
        [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Position=1)]
        [string[]]$Name = $env:COMPUTERNAME
    )
    Begin
    {
        $LMkeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall","SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
        $LMtype = [Microsoft.Win32.RegistryHive]::LocalMachine
        $CUkeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
        $CUtype = [Microsoft.Win32.RegistryHive]::CurrentUser
    }
    Process
    {
        ForEach($Computer in $Name)
        {
            $MasterKeys = @()
            If(!(Test-Connection -ComputerName $Computer -count 1 -quiet))
            {
                Write-Error -Message "Unable to contact $Computer. Please verify its network connectivity and try again." -Category ObjectNotFound -TargetObject $Computer
                Break
            }
            $CURegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($CUtype,$computer)
            $LMRegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($LMtype,$computer)
            ForEach($Key in $LMkeys)
            {
                $RegKey = $LMRegKey.OpenSubkey($key)
                If($RegKey -ne $null)
                {
                    ForEach($subName in $RegKey.getsubkeynames())
                    {
                        foreach($sub in $RegKey.opensubkey($subName))
                        {
                            $MasterKeys += (New-Object PSObject -Property @{
                            "ComputerName" = $Computer
                            "Name" = $sub.getvalue("displayname")
                            "SystemComponent" = $sub.getvalue("systemcomponent")
                            "ParentKeyName" = $sub.getvalue("parentkeyname")
                            "Version" = $sub.getvalue("DisplayVersion")
                            "UninstallCommand" = $sub.getvalue("UninstallString")
                            })
                        }
                    }
                }
            }
            ForEach($Key in $CUKeys)
            {
                $RegKey = $CURegKey.OpenSubkey($Key)
                If($RegKey -ne $null)
                {
                    ForEach($subName in $RegKey.getsubkeynames())
                    {
                        foreach($sub in $RegKey.opensubkey($subName))
                        {
                            $MasterKeys += (New-Object PSObject -Property @{
                            "ComputerName" = $Computer
                            "Name" = $sub.getvalue("displayname")
                            "SystemComponent" = $sub.getvalue("systemcomponent")
                            "ParentKeyName" = $sub.getvalue("parentkeyname")
                            "Version" = $sub.getvalue("DisplayVersion")
                            "UninstallCommand" = $sub.getvalue("UninstallString")
                            })
                        }
                    }
                }
            }
            $MasterKeys = ($MasterKeys | Where {$_.Name -ne $Null -AND $_.SystemComponent -ne "1" -AND $_.ParentKeyName -eq $Null} | select ComputerName,Name,Version | sort Name)
            $MasterKeys
        }
    }
    End
    {

    }

0 个答案:

没有答案