Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Windows API wrappers for Go - useful for SysOps

License

NotificationsYou must be signed in to change notification settings

iamacarpet/go-win64api

Repository files navigation

For System Info / User Management.

For an internal project, this is a set of wrappers for snippets of the Windows API.

Tested and developed for Windows 10 x64.

All functions that return useful data, do so in the form of JSON exportable structs.

These structs are available in the shared library, "github.com/iamacarpet/go-win64api/shared"

Process List

package mainimport ("fmt"    wapi"github.com/iamacarpet/go-win64api")funcmain(){pr,err:=wapi.ProcessList()iferr!=nil {fmt.Printf("Error fetching process list... %s\r\n",err.Error())    }for_,p:=rangepr {fmt.Printf("%8d - %-30s - %-30s - %s\r\n",p.Pid,p.Username,p.Executable,p.Fullpath)    }}

Active Session List (Logged in users + Run-As users)

package mainimport ("fmt"    wapi"github.com/iamacarpet/go-win64api")funcmain(){// This check runs best as NT AUTHORITY\SYSTEM//// Running as a normal or even elevated user,// we can't properly detect who is an admin or not.//// This is because we require TOKEN_DUPLICATE permission,// which we don't seem to have otherwise (Win10).users,err:=wapi.ListLoggedInUsers()iferr!=nil {fmt.Printf("Error fetching user session list.\r\n")return    }fmt.Printf("Users currently logged in (Admin check doesn't work for AD Accounts):\r\n")for_,u:=rangeusers {fmt.Printf("\t%-50s - Local User: %-5t - Local Admin: %t\r\n",u.FullUser(),u.LocalUser,u.LocalAdmin)    }}

Installed Software List

package mainimport ("fmt"    wapi"github.com/iamacarpet/go-win64api")funcmain(){sw,err:=wapi.InstalledSoftwareList()iferr!=nil {fmt.Printf("%s\r\n",err.Error())    }for_,s:=rangesw {fmt.Printf("%-100s - %s - %s\r\n",s.Name(),s.Architecture(),s.Version())    }}

Windows Update Status

package mainimport ("fmt""time"        wapi"github.com/iamacarpet/go-win64api")funcmain() {ret,err:=wapi.UpdatesPending()iferr!=nil {fmt.Printf("Error fetching data... %s\r\n",err.Error())        }fmt.Printf("Number of Updates Available: %d\n",ret.NumUpdates)fmt.Printf("Updates Pending:             %t\n\n",ret.UpdatesReq)fmt.Printf("%25s | %25s | %s\n","EVENT DATE","STATUS","UPDATE NAME")for_,v:=rangeret.UpdateHistory {fmt.Printf("%25s | %25s | %s\n",v.EventDate.Format(time.RFC822),v.Status,v.UpdateName)        }}

Local Service Management

List Services

package mainimport ("fmt"    wapi"github.com/iamacarpet/go-win64api")funcmain(){svc,err:=wapi.GetServices()iferr!=nil {fmt.Printf("%s\r\n",err.Error())    }for_,v:=rangesvc {fmt.Printf("%-50s - %-75s - Status: %-20s - Accept Stop: %-5t, Running Pid: %d\r\n",v.SCName,v.DisplayName,v.StatusText,v.AcceptStop,v.RunningPid)    }}

Start Service

err:=wapi.StartService(service_name)

Stop Service

err:=wapi.StopService(service_name)

Local User Management

List Local Users

package mainimport ("fmt""time"    wapi"github.com/iamacarpet/go-win64api")funcmain(){users,err:=wapi.ListLocalUsers()iferr!=nil {fmt.Printf("Error fetching user list, %s.\r\n",err.Error())return    }for_,u:=rangeusers {fmt.Printf("%s (%s)\r\n",u.Username,u.FullName)fmt.Printf("\tIs Enabled:                   %t\r\n",u.IsEnabled)fmt.Printf("\tIs Locked:                    %t\r\n",u.IsLocked)fmt.Printf("\tIs Admin:                     %t\r\n",u.IsAdmin)fmt.Printf("\tPassword Never Expires:       %t\r\n",u.PasswordNeverExpires)fmt.Printf("\tUser can't change password:   %t\r\n",u.NoChangePassword)fmt.Printf("\tPassword Age:                 %.0f days\r\n", (u.PasswordAge.Hours()/24))fmt.Printf("\tLast Logon Time:              %s\r\n",u.LastLogon.Format(time.RFC850))fmt.Printf("\tBad Password Count:           %d\r\n",u.BadPasswordCount)fmt.Printf("\tNumber Of Logons:             %d\r\n",u.NumberOfLogons)    }}

Adding a Local User

ok,err:=wapi.UserAdd(username,fullname,password)

Deleting a Local User

ok,err:=wapi.UserDelete(username)

Set Full Name Attribute

ok,err:=wapi.UserUpdateFullname(username,fullname)

Give Admin Privileges

ok,err:=wapi.SetAdmin(username)

Revoke Admin Privileges

ok,err:=wapi.RevokeAdmin(username)

Disable/Enable User

s:=true// disable users:=false// enable userok,err:=wapi.UserDisabled(username,s)

Change Attribute - User Can't Change Password

s:=true// User can't change passwords:=false// User can change passwordok,err:=wapi.UserDisablePasswordChange(username,s)

Change Attribute - Password Never Expires

s:=true// Password never expires.s:=false// Enable password expiry.ok,err:=wapi.UserPasswordNoExpires(username,s)

Forced Password Change

ok,err:=wapi.ChangePassword(username,newpassword)

Windows Firewall - Add Inbound Rule

added,err:=wapi.FirewallRuleCreate("App Rule Name","App Rule Long Description.","My Rule Group","%systemDrive%\\path\\to\\my.exe","port number as string",wapi.NET_FW_IP_PROTOCOL_TCP,)

[8]ページ先頭

©2009-2025 Movatter.jp