gpt4 book ai didi

mysql - Powershell - 插入 MySQL 数据库表错误?

转载 作者:行者123 更新时间:2023-11-29 12:37:48 25 4
gpt4 key购买 nike

我有这个插入查询,它没有正确插入,而且真的很烦人。我将简要解释我的尝试以及我真正想要实现的目标。

我的 PS 脚本的作用:

  1. 从数据库表 ping 每台计算机
  2. 如果 ping 成功,则获取从计算机登录的用户名
  3. 将以下值插入数据库表:用户名、主机名、在线(true/false)

现在这是不断出现的问题。

问题 1:如果我使用以下命令:

$username = Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem |选择对象-展开属性用户名;

它将插入用户名值,如下所示:SUPER/user1,这是好的,但我想去掉“SUPER/”并在插入之前保留实际的用户名“user1”。

我尝试使用下面的,但它不起作用:

 $username =  Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object -ExpandProperty -replace '^.*\\'  Username;  

问题 1 中的命令会给我以下两个错误:

无法处理参数,因为参数“obj”的值为 null。改变参数“obj”的值为非空值。

还有

Get-WmiObject:RPC 服务器不可用。

如何忽略这两个错误?

我希望有人能帮助我,因为这真的很烦人。预先感谢大家。

Powershell 脚本:

foreach($pcname in $workstation_list){

$command = $connection.CreateCommand();
$command.Connection = $connection;
$command.CommandText = "INSERT INTO workstation_userlogged
(username,hostname,online)
VALUES (@username,@hostname,@status)";

### =============================================================
### values to be assigned as a parameters
### =============================================================
$workstation = $pcname;
$test_ping = Test-Connection -ComputerName $workstation -Count 1 -ErrorAction SilentlyContinue;
$status = [bool](Test-Connection $workstation -Quiet -Count 1 -ErrorAction SilentlyContinue);

#if status is TRUE get username
if($test_ping)
{
$username = Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object -ExpandProperty Username;
echo $username;
#echo -replace '^.*\\' $username;

#if ping succeeds but no user is logged
if($username -eq "")
{
$username = "no user logged";
}

} #end if

#if ping DOES NOT succeed set username value to NULL and status to FALSE
elseif(!$test_ping)
{
$username = [DBNull]::Value;
}

### =============================================================
### Assign parameters with appropriate values
### =============================================================
$command.Parameters.Add("@username", $username) | Out-Null
$command.Parameters.Add("@hostname", $workstation) | Out-Null
$command.Parameters.Add("@status", $status) | Out-null
### =============================================================
### Execute command query
### =============================================================
try{
$command.ExecuteNonQuery() | out-null;
Write-Host "Successfully inserted in Database";
}
catch{
Write-Host "Caught the exception";
Write-Host "$_";
}

### =============================================================
### clear parameter values
### =============================================================
$command.Dispose()
$workstation = "";
$username = "";
$status = "";
$test_ping = "";
}#end for each loop

最佳答案

我看到两件事。首先,这一行:

$username = Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object Username #-ExpandProperty

应该是这样的:

$username = Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object -ExpandProperty Username 

其次,在 SQL 字符串中嵌入用户名变量时没有使用任何引号。尝试这个。我引用了所有三个值,因为我不知道其他列是什么类型。

$command.CommandText = "INSERT INTO workstation_userlogged
(username,hostname,online)
VALUES ('$username','$pc_db','$ping_status')";

关于mysql - Powershell - 插入 MySQL 数据库表错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26513105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com