powershell3

2018-07-01 03:40:13 | powershell
#定数宣言
Set-Variable -name XAML_FILE -value "combo.xaml" -Option Constant -Scope script
Set-Variable -name CSVOUTPUT_FILE -value "output.csv" -Option Constant -Scope script

$Datas = @()


$path = $MyInvocation.MyCommand.Path | Split-Path -Parent
$xaml = [System.IO.File]::ReadAllText($path + "\" + $XAML_FILE, [System.Text.Encoding]::UTF8)
$form = [System.Windows.Markup.XamlReader]::Parse($xaml)

$comboBox = $form.FindName("Combo1")
$configJson = $path + "\config.json"
$obj = Get-Content $configJson -Encoding UTF8 -Raw | ConvertFrom-Json

$outputFilePath = $path +"\" + $CSVOUTPUT_FILE

# fill the combobox with some powershell objects
$comboBox.ItemsSource = $obj.契約顧客リスト
# tell the combobox to use the property "DisplayName" to display the object in its list
$comboBox.DisplayMemberPath = 'Name'
$comboBox.SelectedValuePath = "Id"
# tell the combobox to preselect the first element
$comboBox.SelectedIndex = 0

$listView = $form.FindName("listView")
$listView.ItemsSource = @($comboBox.SelectedItem.請求内訳)

$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.Filter = "CSVファイル(*.csv)|*.csv"
$dialog.Title = "ファイルを選択してください。"
$fileSelectBtn = $form.FindName("fileSelectBtn")
$selectFilePath = $form.FindName("selectFilePath")
$fileSelect = $form.FindName("selectFilePath")
$fileUploadBtn = $form.FindName("uploadBtn")

$userid = $form.FindName("userId")
$password = $form.FindName("password")
$proxyUserid = $form.FindName("proxyUserId")
$proxyPassword = $form.FindName("proxyPassword")
$useridPasswordBtn = $form.FindName("useridPasswordBtn")


function uploadFunc($inputFilePath) {

$parms = "/c " + $path + "\test.bat"
$inputObject = Start-Process -FilePath cmd.exe -ArgumentList $parms -PassThru
Wait-Process -InputObject $inputObject

$parms = "/c " + $path + "\test2.bat"
$inputObject = Start-Process -FilePath cmd.exe -ArgumentList $parms -PassThru
Wait-Process -InputObject $inputObject



}

function processExecute( $executePgm, $argument, $workingDir) {

$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $executePgm
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $argument
$pinfo.WorkingDirectory = $workingDir
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()

return $p
}

$fileSelectBtn.Add_Click({

$selectFilePath.Text = $dialog.FileName
if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$fileSelect.Text = $dialog.FileName
}
})

$fileUploadBtn.Add_Click({
if ([System.Windows.Forms.MessageBox]::Show("アップロードを行いますか?","アップロード","YesNo") -eq [System.Windows.Forms.DialogResult]::YES) {
uploadFunc $fileSelect.Text
}
})

$comboBox.Add_SelectionChanged({
$listView.ItemsSource = @($comboBox.SelectedItem.請求内訳)
})

$useridPasswordBtn.Add_Click({
if ([System.Windows.Forms.MessageBox]::Show("ユーザID、パスワードの設定を行いますか?","ユーザID・パスワード設定","YesNo") -eq [System.Windows.Forms.DialogResult]::YES) {
if ($userid.Text -eq "") {
[System.Windows.Forms.MessageBox]::Show("ユーザIDを入力してください。","エラー","OK","Error")
return
}
if ($password.Password -eq "") {
[System.Windows.Forms.MessageBox]::Show("パスワードを入力してください。","エラー","OK","Error")
return
}
if ($proxyUserid.Text -eq "") {
[System.Windows.Forms.MessageBox]::Show("プロキシユーザIDを入力してください。","エラー","OK","Error")
return
}
if ($proxyPassword.Password -eq "") {
[System.Windows.Forms.MessageBox]::Show("プロキシパスワードを入力してください。","エラー","OK","Error")
return
}

Set-Item env:JAVA_HOME -Value "C:\Program Files (x86)\Java\jre1.8.0_171"
## パスワード暗号化
$argument = "/c encrypt.bat -e " + $password.Password
$p = processExecute "cmd.exe" $argument "C:\Program Files (x86)\salesforce.com\Data Loader\bin"
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
Write-Host "exit code: " + $p.ExitCode

## プロキシパスワード暗号化
$argument = "/c encrypt.bat -e " + $proxyPassword.Password
$p = processExecute "cmd.exe" $argument "C:\Program Files (x86)\salesforce.com\Data Loader\bin"
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
Write-Host "exit code: " + $p.ExitCode
}
})

$form.ShowDialog()

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="コンボボックス with PowerShell" Width="500"
Height="300"
>
<TabControl>
<TabItem Header="CSVアップロード">
<Canvas>
<Label Content="契約顧客" Width="80" Margin="12,12,0,0">
</Label>
<ComboBox Name="Combo1" Margin="82,12,0,0">
</ComboBox>
<Button Content="ファイル選択" Height="23" Width="100" Name="fileSelectBtn" Margin="12,50,0,0">
</Button>
<Button Content="アップロード" Height="23" Width="100" Name="uploadBtn" Margin="132,50,0,0">
</Button>
<TextBox Text="" Width="340" Margin="12,80,0,0" Name="selectFilePath" IsReadOnly="True"
TextWrapping="Wrap" BorderThickness="0" >
</TextBox>
<ListView Name="listView" Margin="12,110,0,0" Height="100">

<GridView>
<GridViewColumn Header="請求内訳名" DisplayMemberBinding="{Binding 請求内訳名}" Width="300"/>
<GridViewColumn Header="仕入単価" DisplayMemberBinding="{Binding 仕入単価}" Width="70"/>
<GridViewColumn Header="請求単価" DisplayMemberBinding="{Binding 請求単価}" Width="70"/>
</GridView>

</ListView>
</Canvas>
</TabItem>
<TabItem Header="初期設定">
<Canvas>
<Label Content="ユーザID" Width="100" Margin="12,12,0,0">
</Label>
<TextBox Text="" Width="340" Margin="122,15,0,0" Name="userId" >
</TextBox>
<Label Content="パスワード" Width="100" Margin="12,52,0,0">
</Label>
<PasswordBox Width="340" Margin="122,55,0,0" Name="password" >
</PasswordBox>
<Label Content="プロキシユーザID" Width="100" Margin="12,92,0,0">
</Label>
<TextBox Text="" Width="340" Margin="122,95,0,0" Name="proxyUserId" >
</TextBox>
<Label Content="プロキシパスワード" Width="100" Margin="12,132,0,0">
</Label>
<PasswordBox Width="340" Margin="122,135,0,0" Name="proxyPassword" >
</PasswordBox>
<Button Content="ユーザID・パスワード設定" Height="23" Width="150" Name="useridPasswordBtn" Margin="12,175,0,0">
</Button>
</Canvas>
</TabItem>
</TabControl>


</Window>

{
"契約顧客リスト": [
{"Id":1, "Name":"XXXXXXXXXXX(株)",
"請求内訳": [
{"内訳コード":"010", "請求内訳コード":"2420", "請求内訳名":"XXXXXXXXXXXXX", "仕入単価":6, "請求単価":8, "請求除外":"false"},
{"内訳コード":"010", "請求内訳コード":"2420", "請求内訳名":"XXXXXXXXXXXXX", "仕入単価":13, "請求単価":16, "請求除外":"false"}
]
},
{"Id":2, "Name":"XXXXXXXXXXX(株)",
"請求内訳": [
{"内訳コード":"010", "請求内訳コード":"2420", "請求内訳名":"XXXXXXXXXXXXX", "仕入単価":6, "請求単価":8, "請求除外":"false"},
{"内訳コード":"010", "請求内訳コード":"2420", "請求内訳名":"XXXXXXXXXXXXX", "仕入単価":13, "請求単価":18, "請求除外":"false"}
]
}
]
}


コメントを投稿